网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  使用xml文件来实现对servlet的配置     
  文章作者:未知  文章来源:水木森林  
  查看:99次  录入:管理员--2007-11-17  
 
  我们在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中的动态注入???)
 
 
上一篇: xml(元标记语言)学习基础一    下一篇: 实用技巧分析——jxpath实用讲解
  相关文档
compile 方法 11-16
j2me开发经验:j2me应用程序内存优化三招 (1) 10-24
使用消息框 11-16
java的特点 11-17
copyfile 方法 11-16
使用jfreechart创建图象 11-17
java 的多线程浅析 11-16
拷贝文件的源代码 11-16
j2ee13种核心技术 11-17
学习struts的好资料struts 11-17
快速上手eclipse eclipse快捷键指南 11-17
java 的 class path 和 package(1) 11-17
java 程序编码规范与技巧 11-17
java的核心升级 jdk 6 + jre 6 update 4 03-26
实例讲解如何利用hibernate开发blog 03-24
java基础:mvc设计模式减少编程复杂性 11-16
java动画编程基础第二部分 11-17
310-025 braindumps 11-17
使用midp2.0开发游戏(4)改进的地图生成方式 11-17
开发实体ejb 增强的性能和开发 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息