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:
/*
* Created on 2005-8-29
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package ZY. Pro. WD. servlet;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. SQL. datasource;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
/**
* @ Author zhangyi
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class servletdemo extends httpservlet {
String message;
Datasource Ds;
/**
* Constructor of the object.
*/
Public servletdemo (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}
/**
* The doget method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to get.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out
. Println ("<! Doctype HTML public/"-// W3C // dtd html 4.01 transitional // en/"> ");
Out. println ("<HTML> ");
Out. println ("Out. println ("<body> ");
Out. Print ("this is ");
Out. Print (this. getclass ());
Out. println (", using the get method <br> ");
Out. println (this. getservletconfig (). getinitparameter ("message "));
Out. println ("</body> ");
Out. println ("Out. Flush ();
Out. Close ();
}
/**
* The dopost method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void Init () throws servletexception {
// Put your code here
}
}
In this servlet, we define two attributes: Message and DS. We now make the following configuration in Web. xml:
<Servlet>
<Description>
This is the description of my J2EE component
</Description>
<Display-Name>
This is the display name of my J2EE component
</Display-Name>
<Servlet-Name> servletdemo </servlet-Name>
<Servlet-class> ZY. Pro. WD. servlet. servletdemo </servlet-class>
<Init-param>
<Description> initialize the field of message </description>
<Param-Name> message </param-Name>
<Param-value>
Welcome here, thank you for visiting !!!
</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:
Welcome here, thank you for visiting !!!
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.
(If multiple attributes can be configured, it is not a bit like Dynamic Injection in spring ???)