之前已经讲过get或者post方法的中文乱码问题,之前都是在每个方法中编写设置编码。如果程序变大,就会很繁琐,使用filter可以避免这种繁琐。

1)写一个encodingFilter进行编码设置

public class encodingFilter implements Filter {

    @Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
arg2.doFilter(new encodingRequestServlet(request), response);
} @Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub } @Override
public void destroy() {
// TODO Auto-generated method stub }
}

2)在该方法中使用了一个继承自 HttpServletRequestWrapper 的类,该类可以覆盖HttpServletRequest 类中的方法

我们只需要重写 getParameter 方法即可。

public class encodingRequestServlet extends HttpServletRequestWrapper {
private HttpServletRequest request; public encodingRequestServlet(HttpServletRequest request) {
super(request);
this.request = request;
} @Override
public String getParameter(String name) {
String value = super.getParameter(name);
if(value == null)
return value;
String method = request.getMethod();
if("get".equalsIgnoreCase(method)){
try {
value = new String(value.getBytes("ISO8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} return value;
}
}

过滤器小结:

过滤器使用场景,所有方法需要使用的公共操作的集合,比如所有请求、响应要设置编码就可以用过滤器。

过滤器是处理所有请求,包含静态资源、动态资源。

处理流程:

1)Filter的创建和销毁由WEB服务器负责。 web 应用程序启动时,web 服务器将创建Filter 的实例对象,并调用其init方法,读取web.xml配置,完成对象的初始化功能,从而为后续的用户请求作好拦截的准备工作(filter对象只会创建一次,init方法也只会执行一次)。开发人员通过init方法的参数,可获得代表当前filter配置信息的FilterConfig对象。

2)public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException;//拦截请求 这个方法完成实际的过滤操作。当客户请求访问与过滤器关联的URL的时候,Servlet过滤器将先执行doFilter方法。FilterChain参数用于访问后续过滤器。

3)public void destroy();//销毁 Filter对象创建后会驻留在内存,当web应用移除或服务器停止时才销毁。在Web容器卸载 Filter 对象之前被调用。该方法在Filter的生命周期中仅执行一次。在这个方法中,可以释放过滤器使用的资源。

方法3)在后面学到spring时候,可以使用spring提供的一个encoding filter

在web.xml配置文件中进行配置

<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-</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

java web中get请求中文乱码在filter中解决的更多相关文章

  1. Windows平台下在Emacs中使用plantuml中文乱码问题(已解决)

    Windows平台下在Emacs中使用plantuml中文乱码问题(已解决) */--> code {color: #FF0000} pre.src {background-color: #00 ...

  2. Java web后台插入数据库中文乱码问题解决

    前言:项目想要避免乱码情况的出现,要保持服务器.数据库.项目.以及前端编码一致.一.项目以及前端编码,设置. myeclipse--->右键项目--->Properties--->R ...

  3. Get请求中文乱码的几种解决方式

    1.将字符串转码:new String("xxxxx".getBytes("iso-8859-1"),"utf-8")         这种 ...

  4. RStudio中,出现中文乱码问题的解决方案

    RStudio中,出现中文乱码问题的解决方案解决步骤:1.设置RStudio文本显示的默认编码:RStudio菜单栏的Tools -> Global Options2.选择General -&g ...

  5. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  6. get请求与post请求中文乱码问题的解决办法

    首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...

  7. Spring-解决请求中文乱码问题

    解决spring请求中文乱码问题 1.web.xml添加编码拦截器 <filter> <filter-name>CharacterEncoding</filter-nam ...

  8. android客户端向服务器发送请求中文乱码的问

    android客户端向服务器发送请求的时候,并将参数保存到数据库时遇到了中文乱码的问题: 解决方法: url = "http://xxxx.com/Orders/saveorder.html ...

  9. 关于jFinal开发中遇到的中文乱码问题解决办法

    关于jFinal开发中遇到的中文乱码问题解决办法 设置tomcat的编码,修改 <Connector port="8080" protocol="HTTP/1.1& ...

随机推荐

  1. 【cocos2d-x制作别踩白块儿】第九期:游戏计时功能(附源代码)

    游戏没有计时,不是坑爹吗? 这一期,我们将来加入游戏计时功能. 1. 定义变量和函数 我们先在HelloWorldScene.h中定义几个变量和函数 long startTime; bool time ...

  2. Eclipse里web的依赖工程部署的简便方法

    用Eclipse开发项目,曾经为依赖工程的部署问题头疼过,用了MyEclipse之后就没有仔细去研究,最近研究了下,还真找到了比较简便的方法,之前都是采用Ant打jar包,copy到web工程,或者通 ...

  3. The maximum number of processes for the user account running is currently , which can cause performance issues. We recommend increasing this to at least 4096.

    [root@localhost ~]# vi /etc/security/limits.conf # /etc/security/limits.conf # #Each line describes ...

  4. python笔记16-执行cmd指令(os.system和os.popen)

    os.system 1.如果想在cmd执行python脚本,可以直接用如下指令 python [xx.py绝对路径] 比如我写了个hello.py的脚本,在脚本里面写入内容:print("h ...

  5. Appium+python自动化22-Appium Desktop

    Appium Desktop 原滋原味的官方文档 Appium Desktop是一款用于Mac.Windows和Linux的开源应用,它提供了Appium自动化服务器在一个漂亮灵活的UI中的强大功能. ...

  6. JAVA Date超强工具类,可直接取代util.Date使用

    package net.maxt.util; import java.text.DateFormat; import java.text.ParseException; import java.tex ...

  7. 负载均衡---在window与linux下配置nginx

    最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...

  8. 数学图形(1.26)Clairaut曲线

    像瓜子样的曲线 相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 #http://www.mathcurve.com/cour ...

  9. 手势 触摸【缩放】GestureDetector MotionEvent 案例

    GestureDetector和ScaleGestureDetector示例 /**  * 演示[单点触摸手势识别器]  * 演示[缩放手势识别器]最简单的使用  * @author 白乾涛  */ ...

  10. Windows MongoDB安装配置

    1.下载 官网:http://www.runoob.com/mongodb/mongodb-window-install.html 由于是在window下,所以我下载的是mongodb-win32-x ...