一、过滤器(Filter):又称拦截器。实现Filter接口的类我们称之为Filter(过滤器或拦截器),Filter可以对用户访问的资源进行拦截。例如:客户端发送请求是,先将请求拦截下来,判断用户是否已登录,如果已登录则放行,反之则进入登录页面。
二、拦截器的使用:
a)、在Filter里面可以用request获得请求消息,用response写入响应消息
b)、chain.doFilter(request, response) 方法放行 目标Servlet使用的是同一个请求和响应
c)、doFilter 方法后面的代码会执行,在目标Servlet 返回响应后执行, 也可以使用同一个请求和响应
三、拦截器的实现过程:
a)、写一个类实现 Filter 接口 , 在doFilter 方法中写功能代码
package com.st.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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.st.bean.UserBean;
public class FilterTest implements Filter {
@Override
public void destroy() {
// 生命周期结束,服务器停止时执行此方法
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
// 请求过滤器拦截的路径时执行此方法
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
//下面三句可以很好的解决乱码问题,需要注意的是这三句尽量放在最上面
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8"); String path = request.getServletPath();
if(path.endsWith("LoginServlet.do")){
arg2.doFilter(request, response);// 如果是以LoginServlet.do结尾则放行,执行LoginServlet
}
else{
UserBean user =(UserBean)request.getSession().getAttribute("USER_INFO");
if(user !=null) //用户已经登录,实现放行操作
arg2.doFilter(request, response);
else //用户没有登录,不放行,跳转到登录页面
request.getRequestDispatcher("/login.jsp").include(request, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// 生命周期开始,服务器启动后就会执行此方法
}
}
b)、在web.xml中配置Filter拦截的资源路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>FilterService</servlet-name>
<servlet-class>com.st.service.FilterService</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.st.service.LoginServlet</servlet-class>
</servlet>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.st.filter.FilterTest</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping> <servlet-mapping>
<servlet-name>FilterService</servlet-name>
<url-pattern>/FilterService.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>