java之过滤器

Posted 有猿人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java之过滤器相关的知识,希望对你有一定的参考价值。

form.jsp

技术分享图片
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8" import="java.util.*"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <center><h3>用户登录</h3></center>
10 <body style="text-align: center;">
11     <a href="<%=request.getContextPath()%>/CharacterServlet?name=
12     传智播客&password=123456">点击超链接</a>
13     <form action="<%=request.getContextPath() %>/CharacterServlet" method="get">
14          <table border="1" width="600px" cellpadding="0" cellspacing="0" align="center">
15              <tr>
16                  <td height="30px" align="center">用户名:</td>
17                  <td>&nbsp;<input type="text" name="username"/></td>
18              </tr>
19              <tr>
20                  <td height="30px" align="center">&nbsp;码:</td>
21                  <td>&nbsp;<input type="password" name="password"/></td>
22              </tr>
23              <tr>
24                  <td height="30px" colspan="2" align="center">
25                      <input type="submit" value="登录" />&nbsp;&nbsp;&nbsp;&nbsp;
26                      <input type="reset" value="重置" />
27                  </td>
28              </tr>
29          </table>
30     </form>
31 </body>
32 </html>
View Code

过滤器:

技术分享图片
 1 /**
 2  * 
 3  */
 4 package cn.itcast.filter;
 5 
 6 import java.io.IOException;
 7 import java.io.UnsupportedEncodingException;
 8 
 9 import javax.servlet.Filter;
10 import javax.servlet.FilterChain;
11 import javax.servlet.FilterConfig;
12 import javax.servlet.ServletException;
13 import javax.servlet.ServletRequest;
14 import javax.servlet.ServletResponse;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletRequestWrapper;
18 import javax.servlet.http.HttpServletResponse;
19 
20 import com.sun.org.apache.regexp.internal.recompile;
21 
22 /**TODO
23  * @author sqy
24  * 2018年8月5日上午9:46:57
25  */
26 public class CharacterFilter implements Filter{
27     @Override
28     public void init(FilterConfig filterConfig) throws ServletException {
29          
30         
31     }
32      
33     @Override
34     public void doFilter(ServletRequest req, ServletResponse resp,
35             FilterChain chain) throws IOException, ServletException {
36          HttpServletRequest request = (HttpServletRequest) req;
37          HttpServletResponse response = (HttpServletResponse) resp;
38          //拦截所有请求 解决全站中文编码
39          //指定 request 和response的编码
40          request.setCharacterEncoding("utf-8");
41          response.setContentType("text/html;charset=utf-8");
42          
43          CharacterRequest characterRequest = new CharacterRequest(request);
44          chain.doFilter(characterRequest, response);
45         
46     }
47     
48     @Override
49     public void destroy() {
50          
51         
52     }
53 
54      
55     
56 
57      
58     
59     
60 }
61 /**
62  * TODO    针对request对象进行包装
63  * @author sqy
64  * 2018年8月5日上午11:12:18
65  */
66 class CharacterRequest extends HttpServletRequestWrapper{
67 
68     public CharacterRequest(HttpServletRequest request) {
69         super(request);
70     }
71     
72     public String getParameter(String name){
73         String value = super.getParameter(name);
74         if(value==null){
75             return null;
76         }
77         String method = super.getMethod();
78         if("get".equalsIgnoreCase(method)){
79             try {
80                 value = new String(value.getBytes("iso-8859-1"),"utf-8");
81             } catch (UnsupportedEncodingException e) {
82                 e.printStackTrace();
83             }
84             
85         }
86         
87         return value;
88         
89     }
90     
91 }
View Code

处理页面请求:

技术分享图片
 1 /**
 2  * 
 3  */
 4 package cn.itcast.chapter01.servlet;
 5 
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**TODO
11  * @author sqy
12  * 2018年8月5日上午9:42:11
13  */
14 public class CharacterServlet extends HttpServlet{
15     public void doGet(HttpServletRequest request,HttpServletResponse response){
16         System.out.println(request.getParameter("username"));
17         System.out.println(request.getParameter("password"));
18     }
19     
20     public void doPost(HttpServletRequest request,HttpServletResponse response){
21         doGet(request, response);
22     }
23 }
View Code

web.xml

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>chapter01</display-name>
 4   
 5   
 6   <filter>
 7       <filter-name>CharacterFilter</filter-name>
 8       <filter-class>cn.itcast.filter.CharacterFilter</filter-class>
 9   </filter>
10   
11   <filter-mapping>
12       <filter-name>CharacterFilter</filter-name>
13       <url-pattern>/*</url-pattern>
14   </filter-mapping>
15   
16   <servlet>
17     <servlet-name>CharacterServlet</servlet-name>
18     <servlet-class>cn.itcast.chapter01.servlet.CharacterServlet</servlet-class>
19   </servlet>
20   <servlet-mapping>
21     <servlet-name>CharacterServlet</servlet-name>
22     <url-pattern>/CharacterServlet</url-pattern>
23   </servlet-mapping>
24   
25 </web-app>
View Code

 

以上是关于java之过滤器的主要内容,如果未能解决你的问题,请参考以下文章

Java三大器之过滤器(Filter)的工作原理和代码演示

Java三大器之过滤器(Filter)的工作原理和代码演示

Java三大器之过滤器(Filter)的工作原理和代码演示

为什么我不能在此片段中生成唯一对象数组?

面向面试编程代码片段之GC

Java三大器之拦截器(Interceptor)的实现原理及代码示例