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:
- /*
- *Createdon2005-8-29
- *
- *TODOTochangethetemplateforthisgeneratedfilegoto
- *Window-Preferences-Java-CodeStyle-CodeTemplates
- */
- packagezy.pro.wd.servlet;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjavax.sql.DataSource;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- /**//**
- *@authorzhangyi
- *
- *TODOTochangethetemplateforthisgeneratedtypecommentgoto
- *Window-Preferences-Java-CodeStyle-CodeTemplates
- */
- publicclassServletDemoextendsHttpServlet...{
- Stringmessage;
- DataSourceds;
- /**//**
- *Constructoroftheobject.
- */
- publicServletDemo()...{
- super();
- }
- /**//**
- *Destructionoftheservlet.<br>
- */
- publicvoiddestroy()...{
- super.destroy();//Justputs"destroy"stringinlog
- //Putyourcodehere
- }
- /**//**
- *ThedoGetmethodoftheservlet.<br>
- *
- *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
- *
- *@paramrequesttherequestsendbytheclienttotheserver
- *@paramresponsetheresponsesendbytheservertotheclient
- *@throwsServletExceptionifanerroroccurred
- *@throwsIOExceptionifanerroroccurred
- */
- publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException...{
- response.setContentType("text/html");
- PrintWriterout=response.getWriter();
- out.println("<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDHTML4.01Transitional//EN\">");
- out.println("<HTML>");
- out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");
- out.println("<BODY>");
- out.print("Thisis");
- out.print(this.getClass());
- out.println(",usingtheGETmethod<br>");
- out.println(this.getServletConfig().getInitParameter("message"));
- out.println("</BODY>");
- out.println("</HTML>");
- out.flush();
- out.close();
- }
- /**//**
- *ThedoPostmethodoftheservlet.<br>
- *
- *Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
- *
- *@paramrequesttherequestsendbytheclienttotheserver
- *@paramresponsetheresponsesendbytheservertotheclient
- *@throwsServletExceptionifanerroroccurred
- *@throwsIOExceptionifanerroroccurred
- */
- publicvoidinit()throwsServletException...{
- //Putyourcodehere
- }
- }
In this Servlet, we define two attributes: message and ds. We now make the following configuration in web. xml:
- <servlet>
- <description>
- ThisisthedescriptionofmyJ2EEcomponent
- </description>
- <display-name>
- ThisisthedisplaynameofmyJ2EEcomponent
- </display-name>
- <servlet-name>ServletDemo</servlet-name>
- <servlet-class>zy.pro.wd.servlet.ServletDemo</servlet-class>
- <init-param>
- <description>initializethefieldofmessage</description>
- <param-name>message</param-name>
- <param-value>
- welcomehere,thankyouforvisiting!!!
- </param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>ServletDemo</servlet-name>
- <url-pattern>/servlet/ServletDemo</url-pattern>
- </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.
- Servlet Engine installation
- Configure the Servlet Development Environment
- JSP Servlet call in tag Library
- Small issues encountered when learning Java Servlet
- Servlet sharing links in sessions