码迷,mamicode.com
首页 > 其他好文 > 详细

动态代理实现设置tomcat请求编码

时间:2018-04-06 21:22:04      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:except   over   code   throws   sub   gpo   int   gen   return   

1)htmlcode:

<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="/login"  method="post" >
    姓名:<input  type="text" name="username"><br>
    备注:<textarea></textarea>

    <input type="submit"><br>

  </form>
  </body>
</html>

 2)servlet code

 1 package jd.com.coding;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.annotation.WebServlet;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import java.io.IOException;
 9 
10 @WebServlet(name = "Servletlogin" ,urlPatterns ="/login")
11 public class Servletlogin extends HttpServlet {
12     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13         String username=request.getParameter("username");
14         System.out.println(username);
15 
16 
17 
18     }
19 
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21 
22     }
23 }

3)filter代码:

 1 package jd.com.coding;
 2 
 3 import javax.servlet.*;
 4 import javax.servlet.annotation.WebFilter;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.IOException;
 8 import java.lang.reflect.InvocationHandler;
 9 import java.lang.reflect.Method;
10 import java.lang.reflect.Proxy;
11 
12 @WebFilter(filterName = "FilterCoding",urlPatterns = "/login")
13 public class FilterCoding implements Filter {
14     public void destroy() {
15     }
16 
17     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
18         //增强
19         HttpServletResponse response= (HttpServletResponse) resp;
20         HttpServletRequest request= (HttpServletRequest) req;
21 
22         HttpServletRequest reqproxy =(HttpServletRequest) Proxy.newProxyInstance(HttpServletRequest.class.getClassLoader(), (Class<?>[]) request.getClass().getGenericInterfaces(), new InvocationHandler() {
23             @Override
24             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
25                 if(method.getName().equalsIgnoreCase("getParameter")){
26                     if(request.getMethod().equalsIgnoreCase("get")){
27 //                        String[get] ar=(String[]) args;
28 //                        String str=request.getParameter(ar[0]);
29                         String str= (String) method.invoke(request,args);
30                         return  new String(str.getBytes("iso8859-1"),"utf-8");
31                     }else if(request.getMethod().equalsIgnoreCase("post")){
32                         request.setCharacterEncoding("utf-8");
33                         return  method.invoke(request,args);
34                     }
35                 }
36 
37                 return method.invoke(request,args);
38             }
39         });
40         chain.doFilter(reqproxy, resp);
41     }
42 
43     public void init(FilterConfig config) throws ServletException {
44 
45     }
46 
47 }

 

其中获取类的接口集合使用Class类的方法:getClassLoader()

public Type[] getGenericInterfaces()

然后强转即可。

 

动态代理实现设置tomcat请求编码

标签:except   over   code   throws   sub   gpo   int   gen   return   

原文地址:https://www.cnblogs.com/evilliu/p/8728639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!