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