日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何讓XML來配置Servlet

我們在Web應(yīng)用中可以使用xml來配置Servlet,給其提供初始化參數(shù),如下例:

創(chuàng)新互聯(lián)建站專業(yè)提供成都主機(jī)托管四川主機(jī)托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價(jià)格,機(jī)房位于中國電信/網(wǎng)通/移動(dòng)機(jī)房,資陽托管服務(wù)器服務(wù)有保障!

我們創(chuàng)建的Servlet為:ServletDemo.java,代碼如下:

 
 
 
  1. /*  
  2. *Createdon2005-8-29  
  3. *  
  4. *TODOTochangethetemplateforthisgeneratedfilegoto  
  5. *Window-Preferences-Java-CodeStyle-CodeTemplates  
  6. */  
  7. packagezy.pro.wd.servlet;  
  8. importjava.io.IOException;  
  9. importjava.io.PrintWriter;  
  10. importjavax.sql.DataSource;  
  11. importjavax.servlet.ServletException;  
  12. importjavax.servlet.http.HttpServlet;  
  13. importjavax.servlet.http.HttpServletRequest;  
  14. importjavax.servlet.http.HttpServletResponse;  
  15. /**//**  
  16. *@authorzhangyi  
  17. *  
  18. *TODOTochangethetemplateforthisgeneratedtypecommentgoto  
  19. *Window-Preferences-Java-CodeStyle-CodeTemplates  
  20. */  
  21. publicclassServletDemoextendsHttpServlet...{  
  22. Stringmessage;  
  23. DataSourceds;  
  24. /**//**  
  25. *Constructoroftheobject.  
  26. */  
  27. publicServletDemo()...{  
  28. super();  
  29. }  
  30. /**//**  
  31. *Destructionoftheservlet.
     
  32. */  
  33. publicvoiddestroy()...{  
  34. super.destroy();//Justputs"destroy"stringinlog  
  35. //Putyourcodehere  
  36. }  
  37. /**//**  
  38. *ThedoGetmethodoftheservlet.
     
  39. *  
  40. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.  
  41. *  
  42. *@paramrequesttherequestsendbytheclienttotheserver  
  43. *@paramresponsetheresponsesendbytheservertotheclient  
  44. *@throwsServletExceptionifanerroroccurred  
  45. *@throwsIOExceptionifanerroroccurred  
  46. */  
  47. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)  
  48. throwsServletException,IOException...{  
  49. response.setContentType("text/html");  
  50. PrintWriterout=response.getWriter();  
  51. out.println(">");  
  52. out.println("");  
  53. out.println("</strong>AServlet<strong>");  
  54. out.println("");  
  55. out.print("Thisis");  
  56. out.print(this.getClass());  
  57. out.println(",usingtheGETmethod
    ");  
  58. out.println(this.getServletConfig().getInitParameter("message"));  
  59. out.println("");  
  60. out.println("");  
  61. out.flush();  
  62. out.close();  
  63. }  
  64. /**//**  
  65. *ThedoPostmethodoftheservlet.
     
  66. *  
  67. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.  
  68. *  
  69. *@paramrequesttherequestsendbytheclienttotheserver  
  70. *@paramresponsetheresponsesendbytheservertotheclient  
  71. *@throwsServletExceptionifanerroroccurred  
  72. *@throwsIOExceptionifanerroroccurred  
  73. */  
  74. publicvoidinit()throwsServletException...{  
  75. //Putyourcodehere  
  76. }  

在此Servlet中我們定義了兩個(gè)屬性message和ds。我們現(xiàn)在在web.xml中作如下配置:

 
 
 
  1.  
  2.  
  3. ThisisthedescriptionofmyJ2EEcomponent  
  4.  
  5.  
  6. ThisisthedisplaynameofmyJ2EEcomponent  
  7.  
  8. ServletDemo 
  9. zy.pro.wd.servlet.ServletDemo 
  10.  
  11. initializethefieldofmessage 
  12. message 
  13.  
  14. welcomehere,thankyouforvisiting!!!  
  15.  
  16.  
  17.  
  18.  
  19. ServletDemo 
  20. /servlet/ServletDemo 
  21.  

加粗的部分是我們要作的配置。在其中我們給message屬性設(shè)置了初始值:
welcomehere,thankyouforvisiting!!!

注意:此處我們不能同時(shí)給ds設(shè)置初始值,因?yàn)閣eb.xml的DTD中約定了只能定義一個(gè)屬性也就是在配置文件中只允許聲明一個(gè)參數(shù)值對。這樣,在我們的servlet中就可以這樣來訪問此屬性:this.getServletConfig().getInitParameter("message")。但是,有時(shí)候我們需要同時(shí)對多個(gè)屬性用XML來初始化,那么我們就需要自己來寫XML文件,同時(shí)自己來解析了。

使用XML來配置Servlet的好處:
如果不在XML中對Servlet配置,那么我們修改Servlet的屬性的話就要重新啟動(dòng)服務(wù)器,而如果使用XML來配置的話就不需要重新啟動(dòng)服務(wù)器而可以自動(dòng)生效。服務(wù)器可以自動(dòng)監(jiān)視其改變而重新裝入文檔。對企業(yè)來說,系統(tǒng)的連續(xù)運(yùn)營是很重要的。

XML來配置Servlet主要用在初始化參數(shù)在運(yùn)行過程中需要改變的情況下。

【編輯推薦】

  1. Servlet引擎的安裝
  2. 配置Servlet開發(fā)環(huán)境
  3. 標(biāo)簽庫中JSP Servlet調(diào)用
  4. 學(xué)習(xí)Java Servlet時(shí)遇到的小問題
  5. Servlet在session中共享鏈接

名稱欄目:如何讓XML來配置Servlet
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/coojioi.html