Servlet初始配置 监听器和过滤器
Posted shuchen007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet初始配置 监听器和过滤器相关的知识,希望对你有一定的参考价值。
ServletContext:application范围内的参数
此所设定的参
数,在JSP网页中可以使用下列方法来取得:
${initParam.param0},
<%=application.getInitParameter("param0") %><br/>
若在Servlet可以使用下列方法来获得:
String param_name=getServletContext().getInitParameter("param0")
=this.getServletConfig().getServletContext().getInitParameter("param0")
=application.getInitParameter("param0");
使用上下文初始化参数:
1
2
3
4
5
6
7
8
|
<context-param> <param-name>param_name</param-name> <param-value>avalible during application</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-configuration/*.xml</param-value> </context-param> |
部署在同一容器中的多个Web项目,要配置不同的webAppRootKey,web.xml文件中最好定义webAppRootKey参数,如果不定义,将会缺省为“webapp.root”
1
2
3
4
5
6
7
8
9
|
< context-param > < param-name >webAppRootKey</ param-name > < param-value > business.root </ param-value > </ context-param > <!—public-base应用路径 --> < context-param > < param-name >webAppRootKey</ param-name > < param-value > pubbase.root</ param-value > </ context-param > |
在web.xml中该Servlet的定义标记中,比如:
servlet范围内的参数,只能在servlet的init()方法中取得(或者通过注解配置在对应的类上:@WebServlet("/MyServlet")
System.out.println(this.getInitParameter("param1")=this.getServletConfig().getInitParameter(name));
1
2
3
4
5
6
7
8
|
<servlet> <servlet-name>TimeServlet</servlet-name> <servlet- class >com.allanlxf.servlet.basic.TimeServlet</servlet- class > <init-param> <param-name>param1</param-name> <param-value>avalible in servlet init()</param-value> </init-param> <load-on-startup> 0 </load-on-startup> //标记容器是否在启动的时候就加载这个servlet,执行init()方法,初始化参数</servlet> |
配置监听器:(启动Web容器时,自动装配ApplicationContext、log4j的配置信息。)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
< context-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/spring-configuration/*.xml</ param-value > </ context-param > < context-param > < param-name >log4jConfigLocation</ param-name > < param-value >WEB-INF/log4j.properties</ param-value > </ context-param > < context-param > < param-name >log4jRefreshInterval</ param-name > < param-value >6000</ param-value > </ context-param > <!-- Spring的log4j监听器 --> < listener > < listener-class >org.springframework.web.util.Log4jConfigListener</ listener-class > </ listener > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > <!-- 与CAS Single Sign Out Filter配合,注销登录信息 --> < listener > < listener-class >com.yonyou.mcloud.cas.client.session.SingleSignOutHttpSessionListener</ listener-class > </ listener > |
配置过滤器:
Filter可认为是Servle的一种“加强版”,主要用于对用户请求request进行预处理,也可以对Response进行后处理,是个典型的处理链。使用Filter的完整流程是:Filter对用户请求进行预处理,接着将请求HttpServletRequest交给Servlet进行处理并生成响应,最后Filter再对服务器响应HttpServletResponse进行后处理。Filter与Servlet具有完全相同的生命周期,且Filter也可以通过<init-param>来配置初始化参数,获取Filter的初始化参数则使用FilterConfig的getInitParameter()。
换种说法,Servlet里有request和response两个对象,Filter能够在一个request到达Servlet之前预处理request,也可以在离开Servlet时处理response,Filter其实是一个Servlet链。以下是Filter的一些常见应用场合,创建一个过滤器,实现Filter接口。重新init(), doFilter(), destroy().
注解的方式配置:@WebFilter(filterName="FirstFilter",urlPatterns={"/index.jsp"},initParams={WWebInitParam(name="mood",value="awake")})
urlPatterns:哪些URL可以过滤,默认“/*”
initParams: 初始化哪些参数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
<!--****************************过滤器配置*********************************--> <!-- 字符集过滤器 --> < filter > < filter-name >CharacterEncodingFilter</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > <!-- 单点登出过滤器 --> < filter > < filter-name >CAS Single Sign Out Filter</ filter-name > < filter-class >com.yonyou.mcloud.cas.client.session.SingleSignOutFilter</ filter-class > </ filter > <!-- 认证过滤器 --> < filter > < filter-name >CAS Authentication Filter</ filter-name > < filter-class >com.yonyou.mcloud.cas.client.authentication.ExpandAuthenticationFilter</ filter-class > < init-param > < param-name >casServerLoginUrl</ param-name > < param-value >https://dev.yonyou.com:443/sso-server/login</ param-value > </ init-param > < init-param > <!--这里的server是服务端的IP --> < param-name >serverName</ param-name > < param-value >http://10.1.215.40:80</ param-value > </ init-param > </ filter > <!-- 验证ST/PT过滤器 --> < filter > < filter-name >CAS Validation Filter</ filter-name > < filter-class >org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</ filter-class > < init-param > < param-name >casServerUrlPrefix</ param-name > < param-value >https://dev.yonyou.com:443/sso-server</ param-value > </ init-param > < init-param > < param-name >serverName</ param-name > < param-value >http://10.1.215.40:80</ param-value > </ init-param > < init-param > < param-name >proxyCallbackUrl</ param-name > < param-value >https://dev.yonyou.com:443/business/proxyCallback</ param-value > </ init-param > < init-param > < param-name >proxyReceptorUrl</ param-name > < param-value >/proxyCallback</ param-value > </ init-param > < init-param > < param-name >proxyGrantingTicketStorageClass</ param-name > < param-value >com.yonyou.mcloud.cas.client.proxy.MemcachedBackedProxyGrantingTicketStorageImpl</ param-value > </ init-param > <!-- 解决中文问题 --> < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > </ filter > < filter > < filter-name >CAS HttpServletRequest Wrapper Filter</ filter-name > < filter-class >org.jasig.cas.client.util.HttpServletRequestWrapperFilter</ filter-class > </ filter > < filter > < filter-name >CAS Assertion Thread Local Filter</ filter-name > < filter-class >org.jasig.cas.client.util.AssertionThreadLocalFilter</ filter-class > </ filter > < filter > < filter-name >NoCache Filter</ filter-name > < filter-class >com.yonyou.mcloud.cas.client.authentication.NoCacheFilter</ filter-class > </ filter > <!--****************************映射关系配置********************************--> < filter-mapping > < filter-name >CharacterEncodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >NoCache Filter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >CAS Single Sign Out Filter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >CAS Validation Filter</ filter-name > < url-pattern >/proxyCallback</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >CAS Authentication Filter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >CAS Validation Filter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >CAS HttpServletRequest Wrapper Filter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < filter-mapping > < filter-name >CAS Assertion Thread Local Filter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > |
配置失效时间:
1
2
3
|
< session-config > < session-timeout >120</ session-timeout > </ session-config > |
1
2
3
|
< welcome-file-list > </ welcome-file-list ></ span >
|
以上是关于Servlet初始配置 监听器和过滤器的主要内容,如果未能解决你的问题,请参考以下文章