如何让XML来配置Servlet

我们在Web应用中可以使用xml来配置Servlet,给其提供初始化参数,如下例:

创新互联建站专业提供成都主机托管四川主机托管成都服务器托管四川服务器托管,支持按月付款!我们的承诺:贵族品质、平民价格,机房位于中国电信/网通/移动机房,资阳托管服务器服务有保障!

我们创建的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中我们定义了两个属性message和ds。我们现在在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属性设置了初始值:
welcomehere,thankyouforvisiting!!!

注意:此处我们不能同时给ds设置初始值,因为web.xml的DTD中约定了只能定义一个属性也就是在配置文件中只允许声明一个参数值对。这样,在我们的servlet中就可以这样来访问此属性:this.getServletConfig().getInitParameter("message")。但是,有时候我们需要同时对多个属性用XML来初始化,那么我们就需要自己来写XML文件,同时自己来解析了。

使用XML来配置Servlet的好处:
如果不在XML中对Servlet配置,那么我们修改Servlet的属性的话就要重新启动服务器,而如果使用XML来配置的话就不需要重新启动服务器而可以自动生效。服务器可以自动监视其改变而重新装入文档。对企业来说,系统的连续运营是很重要的。

XML来配置Servlet主要用在初始化参数在运行过程中需要改变的情况下。

【编辑推荐】

  1. Servlet引擎的安装
  2. 配置Servlet开发环境
  3. 标签库中JSP Servlet调用
  4. 学习Java Servlet时遇到的小问题
  5. Servlet在session中共享链接

当前题目:如何让XML来配置Servlet
文章来源:http://www.csdahua.cn/qtweb/news22/259672.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网