1
// ==================== Program Discription =====================
2
// 程序名称:示例
14-9 : EncodingFilter .java
3
// 程序目的:学习使用监听器
4
// ==============================================================
5
import javax.servlet.http.*;
6
import javax.servlet.*;
7
8
public class OnLineCountListener implements HttpSessionListener,
ServletContextListener,ServletContextAttributeListener
9
{
10
private int count;
11
private ServletContext context = null;
12
13
public OnLineCountListener()
14
{
15
count=0;
16
//setContext();
17
}
18
//创建一个
session时激发
19
public void sessionCreated(HttpSessionEvent se)
20
{
21
count++;
22
setContext(se);
23
24
}
25
//当一个
session失效时激发
26
public void sessionDestroyed(HttpSessionEvent se)
27
{
28
count--;
29
setContext(se);
30
}
31
//设置
context的属性,它将激发attributeReplaced或attributeAdded方法
32
public void setContext(HttpSessionEvent se)
33
{
34
se.getSession().getServletContext().
setAttribute("onLine",new Integer(count));
35
}
36
//增加一个新的属性时激发
37
public void attributeAdded(ServletContextAttributeEvent event) {
38
39
log("attributeAdded('" + event.getName() + "', '" +
40
event.getValue() + "')");
41
42
}
43
44
//删除一个新的属性时激发
45
public void attributeRemoved(ServletContextAttributeEvent event) {
46
47
log("attributeRemoved('" + event.getName() + "', '" +
48
event.getValue() + "')");
49
50
}
51
52
//属性被替代时激发
53
public void attributeReplaced(ServletContextAttributeEvent event) {
54
55
log("attributeReplaced('" + event.getName() + "', '" +
56
event.getValue() + "')");
57
}
58
//context删除时激发
59
public void contextDestroyed(ServletContextEvent event) {
60
61
log("contextDestroyed()");
62
this.context = null;
63
64
}
65
66
//context初始化时激发
67
public void contextInitialized(ServletContextEvent event) {
68
69
this.context = event.getServletContext();
70
log("contextInitialized()");
71
72
}
73
private void log(String message) {
74
75
System.out.println("ContextListener: " + message);
76
}
77
}
|
<listener>
<listener-class>OnLineCountListener</listener-class>
</listener>
|
<%@ page contentType="text/html;charset=gb2312" %>
|
<font color=red><%=getServletContext().getAttribute("onLine")%></font><br>
|
<form action="exit.jsp" method=post>
<input type=submit value="exit">
</form>
|
<%session.invalidate() ;%>
|
…
ContextListener: contextInitialized()
ContextListener: attributeReplaced('org.apache.
catalina.WELCOME_FILES', '[Ljava.lang.String;@1d98a')
…
ContextListener: attributeAdded('onLine', '1')
ContextListener: attributeReplaced('onLine', '1')
ContextListener: attributeReplaced('onLine', '0')
ContextListener: attributeReplaced('onLine', '1')
ContextListener: attributeReplaced('onLine', '2')
|
destroy() //由
Web容器调用,销毁此Filter
init(FilterConfig filterConfig) ///由
Web容器调用,初始化此Filter
doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)//具体过滤处理代码
|
1
// ==================== Program Discription =====================
2
// 程序名称:示例
14-7 : EncodingFilter .java
3
// 程序目的:学习使用编码过滤器
4
// ==============================================================
5
import javax.servlet.FilterChain;
6
import javax.servlet.ServletRequest;
7
import javax.servlet.ServletResponse;
8
import java.io.IOException;
9
import javax.servlet.Filter;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.ServletException;
13
import javax.servlet.FilterConfig;
14
15
public class EncodingFilter implements Filter
16
{
17
18
private String targetEncoding = "gb2312";
19
protected FilterConfig filterConfig;
20
21
public void init(FilterConfig config) throws ServletException {
22
this.filterConfig = config;
23
this.targetEncoding = config.getInitParameter("encoding");
24
}
25
26
27
public void doFilter(ServletRequest srequest,
ServletResponse sresponse,FilterChain chain)
28
throws IOException, ServletException {
29
30
HttpServletRequest request = (HttpServletRequest)srequest;
31
request.setCharacterEncoding(targetEncoding);//把请求用指定的方式编码
32
// 把处理发送到下一个过滤器
33
chain.doFilter(srequest,sresponse);
34
}
35
36
public void destroy()
37
{
38
this.filterConfig=null;
39
}
40
41
public void setFilterConfig(final FilterConfig filterConfig)
42
{
43
this.filterConfig=filterConfig;
44
}
45
}
|
1
// ==================== Program Discription =====================
2
// 程序名称:示例
14-8 : LoginFilter.java
3
// 程序目的:学习使用登录过滤器
4
// ==============================================================
5
import javax.servlet.FilterChain;
6
import javax.servlet.ServletRequest;
7
import javax.servlet.ServletResponse;
8
import java.io.IOException;
9
import javax.servlet.Filter;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.ServletException;
13
import javax.servlet.FilterConfig;
14
15
public class LoginFilter implements Filter
16
{
17
String LOGIN_PAGE="init.jsp";
18
protected FilterConfig filterConfig;
19
public void doFilter(final ServletRequest req,final ServletResponse
res,FilterChain chain)throws IOException,ServletException
20
{
21
HttpServletRequest hreq = (HttpServletRequest)req;
22
HttpServletResponse hres = (HttpServletResponse)res;
23
String isLog=(String)hreq.getSession().getAttribute("isLog");
24 if((isLog!=null)&&((isLog.equals("true"))||(isLog=="true")))//检查是否登录
25
{
26
chain.doFilter(req,res);
27
return ;
28
}
29
else
30
hres.sendRedirect(LOGIN_PAGE);//如果没有登录,把视图派发到登录页面
31
}
32
33
public void destroy()
34
{
35
this.filterConfig=null;
36
}
37
public void init(FilterConfig config)
38
{
39
this.filterConfig=config;
40
}
41
public void setFilterConfig(final FilterConfig filterConfig)
42
{
43
this.filterConfig=filterConfig;
44
}
45
}
|
<web-app>
<filter>
<filter-name>encoding</filter-name>
<filter-class>EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter>
<filter-name>auth</filter-name>
<filter-class>LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>auth</filter-name>
<url-pattern>/target.jsp</url-pattern>
</filter-mapping>
</web-app>
|
<% session.setAttribute("isLog","true");%>
|
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
|
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/nothing</url-pattern>
</filter-mapping>
|