过滤器能够动态的拦截请求和响应,以变换或使用包含在请求或响应中的信息。html
过滤器是可用于Servlet编程的Java类,能够实现如下目的:java
过滤器经过 Web 部署描述符(web.xml)中的 XML 标签来声明,而后映射到你的应用程序的部署描述符中的 Servlet 名称或 URL 模式。web
当 Web 容器启动 Web 应用程序时,它会为你在部署描述符中声明的每个过滤器建立一个实例。spring
Filter的执行顺序与在web.xml配置文件中的配置顺序一致,通常把Filter配置在全部的Servlet以前。编程
如何编写过滤器?后端
方法介绍:tomcat
对方法中设计到的类介绍:服务器
Web应用程序能够根据特定的目的定义若干个不一样的过滤器,那么就须要在web.xml中对多个过滤器进行多个配置。而在web.xml中使用<filter-mapping>
来控制多个过滤器的执行顺序,即哪一个过滤器的<filter-mapping>
配置在web.xml中的顺序排在前面那这个过滤器就先执行。app
实现不一样的拦截方式须要在中进行不一样的配置:框架
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWORD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
若在web.xml配置文件中没有写出上面四个拦截配置时默认该过滤器只拦截请求。
1.执行目标资源以前作”预处理”工做,例如设置编码,这种一般都会放行,只是在目标资源执行以前作一些准备工做。(例如:几乎是全部的Servlet中都须要写request.setCharacteEncoding()
,能够把它放入到一个Filter中。)这种过滤器没有拦截功能。
2.经过条件判断是否放行,例如校验当前用户是否已经登陆,或者用户IP是否已经被禁用。(有拦截操做) (粗粒度权限控制,会员有会员的权利、游客有游客的权利)
3.在目标资源执行后,作一些后续的特殊处理工做。例如把目标资源输出的数据进行处理。
功能分析:1.统计工做须要在全部资源以前都执行,那么就能够放到Filter中了。2.咱们这个过滤器不打算作拦截操做,由于咱们只是用来作统计的。3.用什么东西来装载统计的数据。Map,整个网站只须要一个Map便可4.Map何时建立(使用ServletContextListener,在服务器启动时完成建立,并保存到SevletContext中),Map保存到哪里:Map须要在Filter中用来保存数据;Map须要在页面使用,打印Map中的数据。
通常咱们经过jsp页面请求转发到servlet时,若请求方式为POST且请求参数包含中文参数时,咱们须要在servlet的doPost()方法中设置POST请求编码问题:request.setCharacterEncoding("utf-8");
、设置响应编码问题:response.setContentType("text/html;charset=utf-8");
,这样即可以解决post请求即响应编码问题;而对于GET请求,若传递的请求参数包含中文参数时设置请求编码就比较麻烦,须要在servlet的doGet()方法中设置响应编码:response.setContentType("text/html;charset=utf-8");
以及请求编码:首先得到传递给servlet的请求参数:String username=request.getParameter("username")
假设传递的请求参数为username
,而后再输入代码username=new String(username.getBytes("ISO8859-1"),"utf-8");
,这样经过jsp页面转发到servlet的参数便解决了编码问题。便可以经过response.getWrite().prinltn(username)
正常显示在网页上。
试想:之后的开发中每每会用到不少的servlet,那咱们岂不是要在每个servlet的doPost()和doGet方法中都写上上述的解决编码代码?这时候咱们就能够经过过滤器来解决了。
首先附上页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>$Title$</title> </head> <body> <a href="<c:url value="/AServlet?username=张三"/> ">点击这里</a> <form action="<c:url value="/AServlet"/> " method="post"> 用户名:<input type="text" name="username" value="李四"> <input type="submit" value="提交"> </form> </body> </html> |
经过”点击这里”的连接咱们便完成了经过jsp页面向servlet发送GET请求参数,经过”提交”按钮咱们便完成了经过jsp页面向servlet发送POST请求参数。建立一个servlet,咱们在servlet中完成响应参数编码的问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class AServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); } } |
接下来在过滤器中完成请求参数编码的问题,建立一个过滤器Filter,在web.xml中注册:
1 2 3 4 5 6 7 8 9 |
<filter> <filter-name>Filter</filter-name> <filter-class>filter.Filter</filter-class> </filter> <filter-mapping> <filter-name>Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
Filter中编码为:
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Filter implements javax.servlet.Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { } public void init(FilterConfig config) throws ServletException { } } |
对于POST请求参数的编码设置咱们直接在doFilter()方法体中添加request.setCharacterEncoding("utf-8");
代码便可(此时运行程序,POST请求参数编码的问题成功解决),对于GET请求参数的编码,有些同窗会以为直接在doFilter()方法体中添加
1 |
String username=request.getParameter("username");username=new String(username.getBytes("ISO-8859-1"),"utf-8"); |
便可。这样的参数是不太靠谱的,由于这里咱们知道要传递的请求参数为username因此这里能够明了的指出,之后咱们不知道请求参数为何或者请求参数有不少时那就须要更多的上诉代码,因此这里咱们采用装饰者模式对request进行装饰(即将原本的request换成咱们本身写的request),建立一个EncodingRequest.java继承HttpServletRequestWrapper,代码以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class EncodingRequest extends HttpServletRequestWrapper { private HttpServletRequest req; public EncodingRequest(HttpServletRequest request) { super(request); this.req=request; } @Override public String getParameter(String name) { String value=req.getParameter(name); //处理编码问题 try { value=new String(value.getBytes("ISO-8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return value; } } |
在构造方法中,咱们传入系统的request,而后将这个request赋值给咱们本身编写的req,而后在重写的getParameter()方法中经过咱们本身写的req获取请求参数并解决编码问题,而后返回解决完编码后的参数value(此时这个中文参数已解决编码),而后在Filer中对咱们本身编写的request(即Encodingquest对象)放行便可。如今doFilter()方法的方法体为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { //处理post请求编码问题 request.setCharacterEncoding("utf-8"); HttpServletRequest req= (HttpServletRequest) request; /** * 处理get请求的编码问题 */ // String username=request.getParameter("username"); // username=new String(username.getBytes("ISO-8859-1"),"utf-8"); /** * 调包request * 1.写一个request的装饰类 * 2.在放行时,使用咱们本身的request */ EncodingRequest er = new EncodingRequest(req); chain.doFilter(er, response); } |
运行程序,成功解决GET请求方式的编码问题,可是POST请求方式的编码又出现了问题,这是为何呢?由于咱们在doFilter方法中已经经过代码request.setCharacterEncoding("utf-8");
处理了POST请求方式的编码问题,可是此时的请求是系统的request对象而不是咱们本身写的req,咱们对req进行了放行而没有对request进行方式,因此方法体中应该增长if判断语句,改正后的doFilter()方法体内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { //处理post请求编码问题 request.setCharacterEncoding("utf-8"); HttpServletRequest req= (HttpServletRequest) request; /** * 处理get请求的编码问题 */ // String username=request.getParameter("username"); // username=new String(username.getBytes("ISO-8859-1"),"utf-8"); /** * 调包request * 1.写一个request的装饰类 * 2.在放行时,使用咱们本身的request */ if (req.getMethod().equals("GET")) { EncodingRequest er = new EncodingRequest(req); chain.doFilter(er, response); }else if (req.getMethod().equals("POST")){ chain.doFilter(request, response); } } |
此时运行程序,成功解决POST请求方式和GET请求方式的编码问题。在学习框架以前咱们都这样经过Filter解决编码问题,而当咱们学习了Spring MVC框架后咱们处理POST请求参数的编码问题时直接在web.xml中添加以下配置而不用再写一个过滤器:
1 2 3 4 5 6 7 8 9 10 11 12 |
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
解决GET请求方式的编码问题时有两种解决方法:1.修改tomcat配置文件添加编码与工程编码一致,以下:
1 |
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/> |
2.对参数进行从新编码:
1 2 |
String userName new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8") |
第二种方法须要对每一个参数都进行从新编码,比较麻烦。
回归咱们的过滤器讲解,经过如上包装request的方式即可以经过过滤器解决全站编码问题。