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

 package jd.com.coding;

 import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebServlet(name = "Servletlogin" ,urlPatterns ="/login")
public class Servletlogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
System.out.println(username); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}

3)filter代码:

 package jd.com.coding;

 import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; @WebFilter(filterName = "FilterCoding",urlPatterns = "/login")
public class FilterCoding implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//增强
HttpServletResponse response= (HttpServletResponse) resp;
HttpServletRequest request= (HttpServletRequest) req; HttpServletRequest reqproxy =(HttpServletRequest) Proxy.newProxyInstance(HttpServletRequest.class.getClassLoader(), (Class<?>[]) request.getClass().getGenericInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equalsIgnoreCase("getParameter")){
if(request.getMethod().equalsIgnoreCase("get")){
// String[get] ar=(String[]) args;
// String str=request.getParameter(ar[0]);
String str= (String) method.invoke(request,args);
return new String(str.getBytes("iso8859-1"),"utf-8");
}else if(request.getMethod().equalsIgnoreCase("post")){
request.setCharacterEncoding("utf-8");
return method.invoke(request,args);
}
} return method.invoke(request,args);
}
});
chain.doFilter(reqproxy, resp);
} public void init(FilterConfig config) throws ServletException { } }

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

public Type[] getGenericInterfaces()

然后强转即可。

动态代理实现设置tomcat请求编码的更多相关文章

  1. AndroidInject项目使用动态代理增加对网络请求的支持

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3540427.html AndroidInject项目是我写的一 ...

  2. 设置tomcat字符编码

    Tomcat的默认编码是ISO-8859-1,如果有是get请求时,会出现乱码,这种情况可以修改Tomcat的编码解决,当然也可以写个过滤器来解决. 在tomcat的conf目录下,编辑server. ...

  3. 二十二 动态代理&解决网站的字符集编码问题

    设计模式: 软件开发过程中,遇到相似问题,将问题的解决方式抽取模型(套路) 单例,工厂,装饰者,适配器,动态代理 谷歌汽车场景: 谷歌汽车场景Car Interface Icar{  start  r ...

  4. 如何设置tomcat服务器编码为utf-8编码

    原文:http://blog.csdn.net/u014079773/article/details/52637057 在实际开发中我们经常遇到request请求的中文乱码,那么如何解决中文乱码问题? ...

  5. 设置tomcat的编码为utf-8

    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" ...

  6. JDK动态代理和CGLIB动态代理编码

    JDK动态代理[接口]: import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import jav ...

  7. 动态代理在WEB与JDBC开发中的应用

    WEB案例 目前有一个2005年开始,基于Struts1的Web项目A,其验证部分依赖于主站的SSO(单点登录).在请求站点A的时候,用户会被强制带去做SSO验证,通过身份验证后后,主站会自动地把请求 ...

  8. Java提高班(六)反射和动态代理(JDK Proxy和Cglib)

    反射和动态代理放有一定的相关性,但单纯的说动态代理是由反射机制实现的,其实是不够全面不准确的,动态代理是一种功能行为,而它的实现方法有很多.要怎么理解以上这句话,请看下文. 一.反射 反射机制是 Ja ...

  9. JDK 原生动态代理是怎么实现的 + 面试题

    JDK 原生动态代理是怎么实现的 + 面试题 反射 反射机制是 Java 语言提供的一种基础功能,赋予程序在运行时自省(introspect)的能力.简单来说就是通过反射,可以在运行期间获取.检测和调 ...

随机推荐

  1. HTML文字闪烁

    <div id="blink">闪烁的文字</div> <script language="javascript"> fun ...

  2. JavaScript高级编程——Date类型

    JavaScript高级编程——Date类型 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...

  3. thymeleaf标签使用方法总结

    https://blog.csdn.net/quuqu/article/details/52511933 常用th标签https://www.cnblogs.com/suncj/p/4030393.h ...

  4. Tornado入门

    一.概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像是一个py文件,不过为了能有效利用非阻塞式服务器环境,这 ...

  5. Flex布局的学习经验

    做为css布局的又一种新方式,Flex拥有极强的使用效果,相比原来的float,position对元素样式的操作更加简洁,本文是我的一点学习经验和心得吧,如有错误以及不足之处,请多多指点. 好进入正题 ...

  6. hudson运行出现java.io.IOException Cannot run program的错误分析

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在昨天运行每日构建时hudson突然出错,错误信息如下: [MySoft3.1] $ cmd /c call &quo ...

  7. Emmagee app性能测试工具使用教程

    Emmagee app性能测试工具使用教程 by:授客 QQ:1033553122 简介 Emmagee是网易杭州研究院QA团队开发的一个简单易上手的Android性能监测小工具,主要用于监控单个Ap ...

  8. profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2

    profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2   by:授客 QQ:103355312 ...

  9. Android 用java语言执行Shell命令

    最近项目中需要用到java语言来执行shell命令,在网上查了资料, 把自己在项目里用到的命令整理成了工具类开放给大家,希望对大家有用.功能不全,后期我会慢慢添加整合. public class Sh ...

  10. SQL Server 2012 列存储索引分析(转载)

    一.概述 列存储索引是SQL Server 2012中为提高数据查询的性能而引入的一个新特性,顾名思义,数据以列的方式存储在页中,不同于聚集索引.非聚集索引及堆表等以行为单位的方式存储.因为它并不要求 ...