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

Posted evil_liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态代理实现设置tomcat请求编码相关的知识,希望对你有一定的参考价值。

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请求编码的主要内容,如果未能解决你的问题,请参考以下文章

使用filter对请求设置编码

nginx+tomcat实现动静分离

ngnix反向代理tomcat偶尔出现请求50x.html错误

Nginx+Tomcat的负载均衡与动静分离集群

HTTPS Tomcat重新路由请求

Nginx+Tomcat实现反向代理与负载均衡入门