load-on-startup

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.

在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是:

标记容器是否在启动的时候就加载这个servlet。

当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;

当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。

正数的值越小,启动该servlet的优先级越高。


-------下面是从[url]http://www.blogjava.net/qiyadeng/articles/13575.html[/url]转过来的在Servlet和JSP中访问web.xml设置的初始参数

在JSP和Servlet中有时候需要读取web.xml的初始化参数,比如可以用于设置JNDI的数据源的名字。下面简单介绍下在JSP和Servlet中怎么读取web.xml的初始化参数。

一、在Servlet中读取:

在Servlet中一般可以用ServletConfig对象的getInitParameter方法去读取指定的参数的值。举个例子:

新建一个Servlet:ReadInitParameter.java

并在web.xml中增加如下配置:


  
<servlet>
<servlet-name>ReadInitParameter</servlet-name>
<servlet-class>com.test.ReadInitParameter</servlet-class>
<init-param> <param-name>email</param-name>
<param-value>qiyadeng@haha.com</param-value>
</init-param>
<init-param>
<param-name>name</param-name>
<param-value>qiyadeng</param-value>
</init-param>
</servlet>


<servletclass>com.test.ReadInitParameter</servlet-class>
是servlet的完整路径,包括包名。

配置好了web.xml,现在可以在程序中读取初始化的参数了。在init(ServletConfig config)方法中传递了一个ServletConfig参数,可以这样获取初始化参数:


 public void init(ServletConfig config) throws ServletException  {    
super.init(config);
String email = config.getInitParameter("email");
String name = config.getInitParameter("name");
}


在其他的方法中象doGet(),doPost(),service()方法中可以用getServeltConfig()方法得到一个ServetConfig对象,在利用该对象的getInitParameter()方法取到对应的初始化参数。


  
String email = getServletConfig().getInitParameter("email");
String name= getServletConfig().getInitParameter("name");


二、在JSP中读取:

在JSP中读取的方式和Servlet中的doGet(),doPost(),Service()的方法一样,都是由ServletConfig对象来读取的。


  <%    
String zhutou = getServletConfig().getInitParameter("zhutou"); String zhutouname = getServletConfig().getInitParameter("zhutouname");

%>


但是,在web.xml的配置需要注意一下,新增如下:


 <servlet>    
<servlet-name>ReadInitParam</servlet-name>
<jsp-file>ReadInitParam.jsp</jsp-file>
<init-param>
<param-name>zhutou</param-name>
<param-value>zhutou@hotmail.com</param-value>
</init-param>
<init-param>
<param-name>zhutouname</param-name>
<param-value>zhutou</param-value>
</init-param>
</servlet>


在servlet-mapping中也要配置好:


 <servlet-mapping>    
<servlet-name>ReadInitParam</servlet-name>
<url-pattern>ReadInitParam.jsp</url-pattern>
</servlet-mapping>


上面的ReadInitParam.jsp出现的标签需要jsp文件的全部路径。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值