How to configure Servlet in XML

Source: Internet
Author: User

In Web applications, we can use xml to configure Servlet and provide initialization parameters for them, as shown in the following example:

The Servlet we created is: ServletDemo. java. The Code is as follows:

 
 
  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.<br> 
  32. */  
  33. publicvoiddestroy()...{  
  34. super.destroy();//Justputs"destroy"stringinlog  
  35. //Putyourcodehere  
  36. }  
  37. /**//**  
  38. *ThedoGetmethodoftheservlet.<br> 
  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("<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");  
  52. out.println("<HTML>");  
  53. out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");  
  54. out.println("<BODY>");  
  55. out.print("Thisis");  
  56. out.print(this.getClass());  
  57. out.println(",usingtheGETmethod<br>");  
  58. out.println(this.getServletConfig().getInitParameter("message"));  
  59. out.println("</BODY>");  
  60. out.println("</HTML>");  
  61. out.flush();  
  62. out.close();  
  63. }  
  64. /**//**  
  65. *ThedoPostmethodoftheservlet.<br> 
  66. *  
  67. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.  
  68. *  
  69. *@paramrequesttherequestsendbytheclienttotheserver  
  70. *@paramresponsetheresponsesendbytheservertotheclient  
  71. *@throwsServletExceptionifanerroroccurred  
  72. *@throwsIOExceptionifanerroroccurred  
  73. */  
  74. publicvoidinit()throwsServletException...{  
  75. //Putyourcodehere  
  76. }  

In this Servlet, we define two attributes: message and ds. We now make the following configuration in web. xml:

 
 
  1. <servlet> 
  2. <description> 
  3. ThisisthedescriptionofmyJ2EEcomponent  
  4. </description> 
  5. <display-name> 
  6. ThisisthedisplaynameofmyJ2EEcomponent  
  7. </display-name> 
  8. <servlet-name>ServletDemo</servlet-name> 
  9. <servlet-class>zy.pro.wd.servlet.ServletDemo</servlet-class> 
  10. <init-param> 
  11. <description>initializethefieldofmessage</description> 
  12. <param-name>message</param-name> 
  13. <param-value> 
  14. welcomehere,thankyouforvisiting!!!  
  15. </param-value> 
  16. </init-param> 
  17. </servlet> 
  18. <servlet-mapping> 
  19. <servlet-name>ServletDemo</servlet-name> 
  20. <url-pattern>/servlet/ServletDemo</url-pattern> 
  21. </servlet-mapping> 

The bold part is the configuration we want to make. Here, we set the initial value for the message attribute:
Welcomehere, thankyouforvisiting !!!

Note: we cannot set the initial value for ds at the same time, because the DTD of web. xml stipulates that only one attribute can be defined, that is, only one parameter value pair can be declared in the configuration file. In this way, we can access this attribute in our servlet as follows: this. getServletConfig (). getInitParameter ("message "). However, sometimes we need to use XML to initialize multiple attributes at the same time, so we need to write the XML file and parse it ourselves.

Advantages of configuring Servlet using XML:
If you do not configure Servlet in XML, You need to restart the server if you modify the Servlet attributes. If you use XML for configuration, you do not need to restart the server and it will take effect automatically. The server can automatically monitor its changes and reload the documents. For enterprises, the continuous operation of the system is very important.

XML to configure Servlet is mainly used when the initialization parameters need to be changed during running.

  1. Servlet Engine installation
  2. Configure the Servlet Development Environment
  3. JSP Servlet call in tag Library
  4. Small issues encountered when learning Java Servlet
  5. Servlet sharing links in sessions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.