<context-param> <init-param> difference and with the role of

2011-10-14  来源:本站原创  分类:Industry  人气:142 

1 Start a WEB project, the container (eg: Tomcat) will read its configuration file web.xml. Read two nodes: <listener> </ listener> and <context-param> </ context-param>
(2) Then, the container creates a ServletContext (context), all parts of this WEB project will share the context.
3 container <context-param> </ context-param> into key-value pairs, and to the ServletContext.
4 containers to create <listener> </ listener> instance of the class is to create monitor.
5 will be in monitoring the contextInitialized (ServletContextEvent args) initialization method, in this way to get ServletContext = ServletContextEvent.getServletContext ();
context-param value = ServletContext.getInitParameter ("context-param key");
6 to get the value of this context-param, you can do some operation. Note that this time you start the WEB project has not been fully completed this action will be early than all of the Servlet.
In other words, this time, you do <context-param> the key operation, will start your WEB project completely before being executed.
7 For example you may want to open the database before the start of the project.
So here you can set up a database connection <context-param> way, in the listener class to initialize the database connection.
8 This monitoring is to write a class, in addition to initialize method, it also destroy method. To close the application before the release of resources such as database connection closed.
Such as:
<! - Spring loaded configuration file ->
<param-name> contextConfigLocation </ param-name>
<param-value> / WEB-INF/applicationContext.xml, / WEB-INF/action-servlet.xml, / WEB-
INF / jason-servlet.xml </ param-value>
</ Context-param>
<listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>
</ Listener>
Another example: ---> Custom context-param, and the custom listener to get this information
<param-name> urlrewrite </ param-name>
<param-value> false </ param-value>
</ Context-param>
<param-name> cluster </ param-name>
<param-value> false </ param-value>
</ Context-param>
<param-name> servletmapping </ param-name>
<param-value> *. bbscs </ param-value>
</ Context-param>
<param-name> poststoragemode </ param-name>
<param-value> 1 </ param-value>
</ Context-param>
<listener-class> com.laoer.bbscs.web.servlet.SysListener </ listener-class>
</ Listener>
public class SysListener extends HttpServlet implements ServletContextListener {
private static final Log logger = LogFactory.getLog (SysListener.class);
public void contextDestroyed (ServletContextEvent sce) {
/ / Used in the container is closed, the operation
/ / Used to open the container, the operation
public void contextInitialized (ServletContextEvent sce) {
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);
The other a little ....
/ * Final output
------------- RootPath: D: \ tomcat_bbs \ webapps \ BBSCS_8_0_3 \
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener] - [INFO]
Application Run Path: D: / tomcat_bbs/webapps/BBSCS_8_0_3 /
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener] - [INFO]
Use Urlrewrite: true
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener] - [INFO]
Use Cluster: false
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener] - [INFO]
2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener] - [INFO]
Post Storage Mode: 1
* /
context-param and init-param difference
two parameters can be defined inside web.xml:
(1) application within the parameters stored in the servletcontext in, configured in web.xml as follows:
<param-name> context / param </ param-name>
<param-value> avalible during application </ param-value>
</ Context-param>
(2) servlet context parameters, only the servlet's init () method to obtain, configured in web.xml as follows:
<servlet-name> MainServlet </ servlet-name>
<servlet-class> com.wes.controller.MainServlet </ servlet-class>
<param-name> param1 </ param-name>
<param-value> avalible in servlet init () </ param-value>
</ Init-param>
<load-on-startup> 0 </ load-on-startup>
</ Servlet>
In the servlet can access the code are:
package com.wes.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class MainServlet extends HttpServlet ... {
public MainServlet () ... {
super ();
public void init () throws ServletException ... {
System.out.println ("param1 following two parameters are stored in the servlet");
System.out.println (this.getInitParameter ("param1"));
System.out.println ("The following parameters are stored in the servletcontext in");
System.out.println (getServletContext (). GetInitParameter ("context / param"));
The first parameter in the servlet which can getServletContext (). GetInitParameter ("context / param") can only be the second parameter in the servlet's init () method by this.getInitParameter ("param1") to obtain.

  • <context-param> <init-param> difference and with the role of 2011-10-14

    1 Start a WEB project, the container (eg: Tomcat) will read its configuration file web.xml. Read two nodes: <listener> </ listener> and <context-param> </ context-param> (2) Then, the container creates a ServletContext (context), a

  • java实现 PageRank算法 2012-07-20

    PageRank算法是Google的核心搜索算法,在所有链接型文档搜索中有极大用处,而且在我们的各种关联系统中都有好的用法,比如专家评分系统,微博搜索/排名,SNS系统等. PageRank算法的依据或思想: 1,被重要的网页链接的越多(外链) ,此网页就越重要 2,此网页对外的链接越少越重要 这两个依据不能是独立的,是需要一起考虑的.但是问题来了,我们怎样判断本网页的外链是很重要的呢?循环判断?那不死循环了? 解决办法是:给定阀值,让循环在此临界处停止. 首先,我们准备了7个测试网页,这几个网

  • jquery.myPagination.js 使用 2012-10-26

    <link type="text/css" rel="stylesheet" href="${css}/pagination.css" /> <script type="text/javascript" src="${js}/jquery.js"></script> <script type="text/javascript" src="

  • Array realization of the collection, intersection, difference 2010-07-13

    /** * Through an array collection, implementation and operation , Hand, the difference * @author BreAthe * */ class SetOperator { /** * SetOne implements array collection, the operation setTwo * @param setOne * @param setTwo */ public void intersecti

  • Difference between the two sets several requirements common algorithm (rpm) 2010-10-21

    There are two arrays: String [] arr01 = {"Andy", "Bill", "Cindy", "Douglas", "Felex", "Green"}; String [] arr02 = {"Andy", "Bill", "Felex", "Green", "

  • 强悍的上下文Context 2013-10-23

    在一个框架中,许多地方都要用到上下文(Context),可以说上下文的实现是否到位,直接关系到开发的便捷性及访问效率. 什么是上下文呢?上下文可以认为是在访问者和被访问者之间进行信息传递的纽带.一般的,我们函数之间调用时通过函数参数及返回值来做做数据的传入和传出,但是,这样就会出现不同访问之间的参数都是不同的,在程序中访问的时候,这当然没有问题.但是对于框架来说,它事件不知道你要用什么方式调用,提供的函数及返回值是什么也不知道,这个时候一般来说,会提供一个上下文(Context)来提供数据传入和

  • GROUP BY, WHERE, HAVING and use of the difference between 2011-04-21

    having clause and where there are similarities but also differences, are setting the conditions statement. The process of aggregation in the query statement (sum, min, max, avg, count) than the having clause takes precedence, while the where clause i

  • Sun look, I used to struts2 Study Notes 2010-05-26

    struts2 interceptor configuration example <struts> <package name="testInterceptor" extends="struts-default"> <! - All the interceptors and interceptor stacks are defined under this element -> <interceptors> <

  • Summary SSH interview questions 2010-12-23

    Summary SSH interview questions MVC 1. Talk about your understanding of mvc MVC is the Model-View-Controler for short. The Model - View - Controller. MVC is a design pattern that the mandatory application of the input, processing and output separatel

  • js package music player (IE, FF) 2009-09-26

    IE only support continuous playback, FF can not play continuously under <! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.0 Transitional / / EN"> <HTML> <HEAD> <TITLE> Music player </ TITLE> <META NAME="Gener

  • Struts-Spring-Hibernate Integration 2009-11-02

    Struts-Spring-Hibernate Integration 6.6.1 Environmental structures and basic configuration Eclipse can we increase the Struts2.0, Spring2.0, Hibernate3.1 support, as follows: 1. To build a simple Struts2 Web Engineering 1) download and install the St

  • Prawn with me to write network (3 days) 2010-03-29

    Implementation User registration and login module Registration Interface: Login interface: 1, define UserService other interface and its implementation /** * User registration * @param user */ public void register(User user); /** * To determine wheth

  • spring configuration files and test classes 2010-03-29

    <? xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = " http://www.springframework.org/schema/beans " xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instance " xmlns: aop = " http://www.springfra

  • struts2 knowledge summary (reproduced) 2010-03-29

    1. The overall logic controller as a filter, is responsible for filtering all requests. The filter API reference category org.apache.struts2.disapatcher. FilterDispatcher 2. MVC: page request (jsp) -> logic controllers (Filter) -> Business Controlle

  • 2010.03.09 (3) - JfreeChart interact with the background of the histogram 2008-09-04

    2010.03.09 (3) - JfreeChart interact with the background of the histogram Can achieve the purpose of this example: There are several pages check box, when you select several of them, the right of the histogram is generated and displayed on the page *

  • Official examples cas 2008-11-05

    The demo guide provides detailed instructions for setting up a multi-domain SSO demonstration for a quick start with CAS. If unreadable in IE (no line wrap), try Firefox or just use the PDF utility. Problem Statement You would like to show-off Single

  • Ext2.0 + struts2 + spring2.5 + ibatis2 + Hsqldb (Oracle) + velocity1.5 + semi-automatic "2" 2008-08-02

    http://www.blogjava.net/ilovezmh/archive/2008/12/27/248570.html The second part velocity1.5 template engine First studied the velocity1.5 template engine is used to automatically generate some code, we know that when the framework of a project to bui

  • Flex version of Mp3 player ---- most of the functional version of one, front 2008-10-03

    Use as a holiday to learn the next flex, personal feeling to do something in order to balance the upper Diansha skilled, to coincide with studio to write songs in a program trying to write one himself, rookie work, lot of criticism. The first to prev

  • Source Tomcat container start --- 6 (3) 2010-03-24

    1, the container has been launched to document the Ministry of summer (webapps), Next was the StandardContext, standardWarpper start there Connector, etc. Let's look at the Department of summer war file / / Deploy WARs, and loop if additional descrip

  • maven filter integrated web project development 2010-03-24

    <!-- Filters the resource file --> <servlet> <servlet-name>PropertiesFilterServlet</servlet-name> <servlet-class> com.taobao.upp.mpc.util.fiter.PropertiesFilterServlet </servlet-class> <init-param> <param-name&