一.过滤器
1.1为什么使用过滤器
设置编码格式的代码:
request.setCharacterEncoding("gb2312");
那么每个JSP页面的内容有中文并且想获取这些内容,则都要写上上面的代码,这是一件很繁琐的事情。为此有人提出:在请求被处理之前,有一个公关的模块,专门负责进行编码的转换。
1.2如何编写过滤器
过滤器只需要在web.xml中配置即可灵活、重复使用,它能够对JSP、HTML、Servlet进行过滤。
编写过滤器步骤如下:
1.实现接口javax.servlet.Filter
2.实现3个方法
方法名 | 描述 |
---|---|
public void init(FilterConfig config) | 表示的是过滤器初始化的动作 |
public void destroy() | 表示的是过滤器消亡时的动作 |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | 过滤器过滤时的动作 |
在项目中建立包filter,建立类EncodingFilter:
EncodingFilter.java
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("gb2312");//设置编码
chain.doFilter(request, response);//将请求向下传递,如果没有这一句,过滤器收到请求进行处理之后,请求将不会传递给目标页面
}
}
1.3如何配置过滤器
在web.xml中加入:
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
即可。
过滤器的配置与Servlet相似。<url-pattern>
用于指定过滤模式。
常见的过滤模式有三种:
1.过滤所有文件
<url-pattern>/*</url-patern>
2.过滤一个或者多个Servlet(JSP)
<filter-mapping>
<filter-name>FilterName</filter-name>
<url-pattern>/PATH1/ServletName1</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>FilterName</filter-name>
<url-pattern>/PATH1/ServletName2</url-pattern>
</filter-mapping>
3.过滤一个或者多个文件目录
<filter-mapping>
<filter-name>FilterName</filter-name>
<url-pattern>/PATH1/*</url-pattern>
</filter-mapping>
<url-pattern>
内部如果以/开头,/表示的是虚拟目录根目录。
二.监听器
Servlet监听器 | 描述 |
---|---|
ServletContextListener | 用于监听ServletContext的变化,如他的创建,销毁 |
ServLetContextAttributeListener | 当一个ServletContext中的属性发生变化,可以用它来监听 |
HttpSessionListener | 监听HttpSession的创建、销毁等变化 |
HttpSessionAttributeListener | 当一个HttpSession中的属性发生变化时可以用它来监听 |
2.1如何编写监听器
2.1.1ServletContextListener的方法
1.ServletContext的初始化
public void contextInitialized(ServletContextEvent sce);
2.ServletContext的消亡
public void contextDestroyed(ServletContextEvent sce);
2.1.2ServletContextAttributeListener的方法
1.增加属性
public void attributeAdded(ServletContextAttributeEvent scab)
2.删除属性
public void attributeRemoved(ServletContextAttributeEvent scab)
3.替换属性
public void attributeReplaced(ServletContextAttributeEvent scab);
2.1.3HttpSessionListener监听器的方法
1.创建HttpSession
public void sessionCreated(HttpSessionEvent se);
2.销毁HttpSession
public void sessionDestroyed(HttpSessionEvent se);
2.1.4HttpSessionAttributeListener监听器的方法
1.增加属性
public void attributeAdded(HttpSessionBindingEvent se);
2.删除属性
public void attributeRemoved(HttpSessionBindingEvent se);
3.替换属性
public void attributeReplaced(HttpSessionBindingEvent se);
2.2编写一个简单的监听器
客户登录成功,实际上是将信息写入session。此时需要自动将其登录信息记录到日志。可以编写一个监听器来实现这个工作,代码如下:
loginSuccess.jsp
<html>
<head>
<title>loginSuccess</title>
</head>
<body>
<%
//模拟登陆成功
session.setAttribute("account", "guokehua");
%>
欢迎<%= session.getAttribute("account") %>登陆成功!
</body>
</html>
loginListener.java
package listener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class loginListener implements HttpSessionAttributeListener{
public void attributeAdded(HttpSessionBindingEvent event){
if(event.getName().equals("account")){
System.out.println("日志信息:" + event.getValue() + "登录!");
}
}
public void attributeRemoved(HttpSessionBindingEvent event){
}
public void attributeReplaced(HttpSessionBindingEvent event){
}
}
从以下代码:
public class loginListener implements HttpSessionAttributeListener{
public void attributeAdded(HttpSessionBindingEvent event){
if(event.getName().equals("account")){
System.out.println("日志信息:" + event.getValue() + "登录!");
}
}
session中放入的信息保存在event参数内。
2.3如何配置监听器
配置监听器很简单,在web.xml中配置其路径就可以了。
<listener>
<listener-class>监听器类路径</listener-class>
</listener>
摘自《Java Web开发与应用》,主编郭克华,副主编宋虹,清华大学出版社