我们在Web应用中可以使用xml来配置Servlet,给其提供初始化参数,如下例:
我们创建的Servlet为:ServletDemo.java,代码如下:
- /*
- *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
- }
- }
在此Servlet中我们定义了两个属性message和ds。我们现在在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>
加粗的部分是我们要作的配置。在其中我们给message属性设置了初始值:
welcomehere,thankyouforvisiting!!!
注意:此处我们不能同时给ds设置初始值,因为web.xml的DTD中约定了只能定义一个属性也就是在配置文件中只允许声明一个参数值对。这样,在我们的servlet中就可以这样来访问此属性:this.getServletConfig().getInitParameter("message")。但是,有时候我们需要同时对多个属性用XML来初始化,那么我们就需要自己来写XML文件,同时自己来解析了。
使用XML来配置Servlet的好处:
如果不在XML中对Servlet配置,那么我们修改Servlet的属性的话就要重新启动服务器,而如果使用XML来配置的话就不需要重新启动服务器而可以自动生效。服务器可以自动监视其改变而重新装入文档。对企业来说,系统的连续运营是很重要的。
XML来配置Servlet主要用在初始化参数在运行过程中需要改变的情况下。