服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用xml文件来实现对servlet的配置


  我们在web应用中可以使用xml来配置servlet,给其提供初始化参数,如下例:
  
  我们创建的servlet为:servletdemo.java,代码如下:
  
  /*
  * created on 2005-8-29
  *
  * todo to change the template for this generated file go to
  * 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 to
  * 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(" <head><title>a servlet</title></head>");
  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("</html>");
  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
  }
  
  }
  
  在此servlet中我们定义了两个属性message和ds。我们现在在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>
  
  加粗的部分是我们要作的配置。在其中我们给message属性设置了初始值:
  
  welcome here ,thank you for visiting !!!
  
  注意:此处我们不能同时给ds设置初始值,因为web.xml的dtd中约定了只能定义一个属性也就是在配置文件中只允许声明一个参数值对。这样,在我们的servlet中就可以这样来访问此属性:
  
  this.getservletconfig().getinitparameter("message")。
  
  但是,有时候我们需要同时对多个属性用xml来初始化,那么我们就需要自己来写xml文件,同时自己来解析了。
  
  使用xml来配置servlet的好处:
  
  如果不在xml中对servlet配置,那么我们修改servlet的属性的话就要重新启动服务器,而如果使用xml来配置的话就不需要重新启动服务器而可以自动生效。服务器可以自动监视其改变而重新装入文档。对企业来说,系统的连续运营是很重要的。
  
  xml来配置servlet主要用在初始化参数在运行过程中需要改变的情况下。
  
  (如果可以实现配置多属性的话,那么不是有点象spring中的动态注入???)

扫描关注微信公众号