一.过滤器
过滤器简介:
过滤器能够对Servlet容器的请求和响应对象进行检查和修改。
过滤器的作用:
过滤器在Servlet被调用之前检查Request对象,修改Request Header和Request内容
过滤器在Servlet被调用之后检查Response对象,修改Response Header和Response内容。
过滤的内容:
Servlet、JSP、HTML等文件
创建过滤器:
所有的过滤器类都必须实现javax.servlet.Filter接口
Filter接口还有3个必须实现的方法:
1.初始化方法
public void init (FilterConfig config)
2.过滤方法
public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain)
3.销毁过滤器方法
public void destroy( )
过滤器的配置,统一设置编码:
开发过滤器(Filter)的步骤:
1.创建类实现Filter接口
2.重写doFilter方法
3.在配置文件(web.xml)中过滤器进行注册
<filter> <filter-name>formFilter</filter-name> <filter-class>com.neusoft.action.FormFilter</filter-class> <init-param> <param-name>encoding</param-name> //对应过滤器中encoding <param-value>UTF-8</param-value> //传递到过滤器 </init-param> </filter> <filter-mapping> <filter-name>formFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
•需求: 使用过滤器修改字符集
form表单登录页面
1 form.jsp登录页面 2 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme() + "://" 7 + request.getServerName() + ":" + request.getServerPort() 8 + path + "/"; 9 %> 10 11 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 12 <html> 13 <head> 14 <base href="<%=basePath%>"> 15 16 <title>My JSP 'form.jsp' starting page</title> 17 18 <meta http-equiv="pragma" content="no-cache"> 19 <meta http-equiv="cache-control" content="no-cache"> 20 <meta http-equiv="expires" content="0"> 21 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 22 <meta http-equiv="description" content="This is my page"> 23 <!-- 24 <link rel="stylesheet" type="text/css" href="styles.css"> 25 --> 26 27 </head> 28 29 <body> 30 <form action="index.jsp" method="post"> 31 提交数据 32 <input type="text" name="username" /> 33 <input type="submit" value="提交" /> 34 </form> 35 </body> 36 </html>
跳转到的页面
1 跳转到index.jsp页面 2 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 4 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 8 %> 9 10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 11 <html> 12 <head> 13 <base href="<%=basePath%>"> 14 15 <title>My JSP 'index.jsp' starting page</title> 16 <meta http-equiv="pragma" content="no-cache"> 17 <meta http-equiv="cache-control" content="no-cache"> 18 <meta http-equiv="expires" content="0"> 19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 20 <meta http-equiv="description" content="This is my page"> 21 <!-- 22 <link rel="stylesheet" type="text/css" href="styles.css"> 23 --> 24 </head> 25 26 <body> 27 您提交的数据是:<c:out value="${param.username}"></c:out> 28 </body> 29 </html>
创建过滤器,实现Filter接口
1 FormFilter过滤器 2 3 package com.neusoft.action; 4 5 import java.io.IOException; 6 7 import javax.servlet.Filter; 8 import javax.servlet.FilterChain; 9 import javax.servlet.FilterConfig; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 14 //过滤器类 15 public class FormFilter implements Filter { 16 17 private String encoding = "iso-8859-1"; 18 private FilterConfig config; 19 public void destroy() { 20 System.out.println("销毁FormFilter.destroy()"); 21 config=null; 22 } 23 24 // 过滤器的处理功能 25 public void doFilter(ServletRequest request, ServletResponse response, 26 FilterChain chain) throws IOException, ServletException { 27 System.out.println("过滤FormFilter.doFilter()"); 28 // 将过滤链上的请求和响应向下一个过滤连传递 29 request.setCharacterEncoding(encoding); 30 chain.doFilter(request, response); 31 } 32 33 public void init(FilterConfig config) throws ServletException { 34 System.out.println("初始化FormFilter.init()"); 35 this.config=config; 36 // 去读web.xml中的初始化参数 37 String s = config.getInitParameter("encoding"); 38 if (s != null) { 39 encoding = s; 40 } 41 42 } 43 44 }
web.xml中配置
1 web.xml 2 3 <?xml version="1.0" encoding="UTF-8"?> 4 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 7 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 8 <welcome-file-list> 9 <welcome-file>index.jsp</welcome-file> 10 </welcome-file-list> 11 12 <!-- 对过滤器注册 --> 13 <filter> 14 <filter-name>formFilter</filter-name> 15 <filter-class>com.neusoft.action.FormFilter</filter-class> 16 <init-param> 17 <param-name>encoding</param-name> 18 <param-value>UTF-8</param-value> 19 </init-param> 20 </filter> 21 <filter-mapping> 22 <filter-name>formFilter</filter-name> 23 <url-pattern>/*</url-pattern> 24 </filter-mapping> 25 26 </web-app>
运行结果
form.jsp表单登录页面
使用过滤器修改字符集运行结果
•需求:"张三"禁止登录,跳转404页面
创建my404.jsp
web.xml中添加
<filter>
<filter-name>StringFilter</filter-name>
<filter-class>com.neusoft.action.StringFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StringFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1 StringFilter处理“张三”禁止登录的过滤器 2 3 package com.neusoft.action; 4 5 import java.io.IOException; 6 7 import javax.servlet.Filter; 8 import javax.servlet.FilterChain; 9 import javax.servlet.FilterConfig; 10 import javax.servlet.ServletException; 11 import javax.servlet.ServletRequest; 12 import javax.servlet.ServletResponse; 13 14 import com.sun.net.httpserver.Filter.Chain; 15 16 public class StringFilter implements Filter { 17 18 public void destroy() { 19 // TODO Auto-generated method stub 20 21 } 22 23 public void doFilter(ServletRequest request, ServletResponse response, 24 FilterChain chain) throws IOException, ServletException { 25 String username = request.getParameter("username"); 26 if ("张三".equals(username)) { 27 //response.getWriter().print("您输入了非法字符"); 28 request.getRequestDispatcher("my404.jsp").forward(request, response); //直接跳转index.jsp主页客户端,看不到跳my404.jsp
29 } else {
30 chain.doFilter(request, response);
31 }
32 }
33
34 public void init(FilterConfig config) throws ServletException {
35 // TODO Auto-generated method stub
36
37 }
38
39 }
运行结果