键者天行

天行键,君子以自强不息;地势坤,君子以厚德载物!

sheng chenID:chensheng913
1198914次访问,排名15好友0人,关注者17
天行键,君子以自强不息;
地势坤,君子以后德载物。
chensheng913的文章
原创 676 篇
翻译 0 篇
转载 85 篇
评论 1013 篇
chensheng913的公告
    首页         留言
本站总访问量:

当前页访问量:


与我联系:我的EMAIL
月 [下月] [上月]

天气预报

最近评论
flying_all:是否是一个公共变量,不仅要看是不是有static关键字,其实更应该看JVM是怎样分配空间的。如果在JVM中一个类的多个实例访问的是同一块内存,那就是公共变量了。谢谢。
flying_all:是否是一个公共变量,不仅要看是不是有static关键字,其实更应该看JVM是怎样分配空间的。如果在JVM中一个类的多个实例访问的是同一块内存,那就是公共变量了。谢谢。
hymcn:不错,学习。
hymcn:好贴!!
hymcn:好贴!!
文章分类
收藏
相册
搞笑娱乐
窈窕淑女
友情Blog
!Java的专栏
Baggico的专栏
feng_sundy的专栏
八进制的专栏
嘟嘟狼的专栏
潇潇的新生活
笨笨的专栏
编程夜未眠的专栏
阿赖的个性空间
韩磊的专栏
珍藏链接
《CSDN社区电子杂志-Java杂志》官方Blog
《CSDN社区电子杂志-Oracle杂志》官方Blog
侯捷专栏
执子之手,与子偕老
王森专栏
科诺专栏
蔡学镛专栏
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

原创 使用XML文件来实现对Servlet的配置收藏

新一篇: Struts的资源绑定 | 旧一篇: Jsp中的session使用

我们在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中的动态注入???)

发表于 @ 2005年08月30日 13:22:00|评论(loading...)|编辑

新一篇: Struts的资源绑定 | 旧一篇: Jsp中的session使用

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © chensheng913