您的位置:首页 > 其它

context-param与init-param的区别与作用

2016-07-13 11:52 302 查看
web.xml的配置中配置作用

1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点:



2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.

3.容器将

转化为键值对,并交给ServletContext.

4.容器创建

中的类实例,即创建监听.

5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext =ServletContextEvent.getServletContext();

context-param的值 =ServletContext.getInitParameter("context-param的键");

6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.

换句话说,这个时候,你对中的键值做的操作,将在你的WEB项目完全启动之前被执行.

7.举例.你可能想在项目启动之前就打开数据库.

那么这里就可以在中设置数据库的连接方式,在监听类中初始化数据库的连接.

8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.

如:

contextConfigLocation

/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-

INF/jason-servlet.xml

org.springframework.web.context.ContextLoaderListener

又如: --->自定义context-param,且自定义listener来获取这些信息

urlrewrite

false

cluster

false

servletmapping

*.bbscs

poststoragemode

1

com.laoer.bbscs.web.servlet.SysListener

public classSysListener extendsHttpServlet implements ServletContextListener {

private staticfinal Log logger = LogFactory.getLog(SysListener.class);

public void contextDestroyed(ServletContextEventsce) {

//用于在容器关闭时,操作

}

//用于在容器开启时,操作

public void contextInitialized(ServletContextEventsce) {

String rootpath = sce.getServletContext().getRealPath("/");

System.out.println("-------------rootPath:"+rootpath);

if(rootpath != null) {

rootpath = rootpath.replaceAll("\\\\","/");

} else {

rootpath = "/";

}

if (!rootpath.endsWith("/")) {

rootpath = rootpath + "/";

}

Constant.ROOTPATH = rootpath;

logger.info("Application Run Path:" + rootpath);

String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");

boolean burlrewrtie = false;

if (urlrewrtie != null) {

burlrewrtie = Boolean.parseBoolean(urlrewrtie);

}

Constant.USE_URL_REWRITE = burlrewrtie;

logger.info("Use Urlrewrite:" + burlrewrtie);

其它略之....

}

}

context-param和init-param区别

web.xml里面可以定义两种参数:

(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:

context/param

avalible during application

(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:

MainServlet

com.wes.controller.MainServlet

param1

avalible in servletinit()

0

在servlet中可以通过代码分别取用:

package com.wes.controller;

importjavax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

public classMainServlet extends HttpServlet ...{

public MainServlet() ...{

super();

}

public void init() throws ServletException ...{

System.out.println("下面的两个参数param1是在servlet中存放的");

System.out.println(this.getInitParameter("param1"));

System.out.println("下面的参数是存放在servletcontext中的");

System.out.println(getServletContext().getInitParameter("context/param"));

}

}

第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到

第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得.

定义

context-param元素含有一对参数名和参数值,用作应用的ServletContext上下文初始化参数。参数名在整个Web应用中必须是惟一的。

编辑本段描述参数

param-name 子元素包含有参数名,而param-value子元素包含的是参数值。作为选择,可用description子元素来描述参数。

下面是一个含有context-param元素的有效部署描述符:

PUBLIC "-//Sun Microsystems, Inc.//DTD WebApplication 2.3//EN"

jdbcDriver

com.mysql.jdbc.Driver

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

这个 load-on-startup 元素 在 web 应用启动的时候指定了servlet被加载的顺序,它的值必须是一个整数。如果它的值是一个负整数或是这个元素不存在,那么容器会在该servlet被调用的时候,加载这个servlet 。如果值是正整数或零,容器在配置的时候就加载并初始化这个servlet,容器必须保证值小的先被加载。如果值相等,容器可以自动选择先加载谁。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: