使用filter解决request.getParameter的中文乱码问题
注意:一般一个站点的所有页面的编码,包括数据库编码都要保持一致,下面默认的编码都是UTF-8
----------------------------------例1:直接提交到jsp页面----------------------------------
input_info.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="display_info.jsp" method="post">
<!-- 输入中文来测试 -->
请输入要显示的内容:<input type="text" name="info">
<input type="submit" value="显示">
</form>
</body>
</html>
display_info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--
注意这里并没有调用request.setCharacterEncoding(),
你把CharacterEncodingFilter关闭了试试,看看结果有什么
不一样
-->
<%
String str = (String) request.getParameter("info");
%>
<h1><%=str%></h1>
</body>
</html>
CharacterEncodingFilter.java
charset参数是从web.xml中配置好的,这里读取进来
public final class CharacterEncodingFilter implements Filter {
private String encoding = null;
private boolean enable = false;
public void destroy() {
this.encoding = null;
}
/**
* 完成request.setCharacterEncoding(encoding);
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (this.enable) {
String encoding = this.selectEncoding(request);
if (encoding != null && !encoding.equals("")) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
// After other filters are done:
if (this.enable) {
String encoding = this.selectEncoding(request);
if (encoding != null && !encoding.equals("")) {
// response.setCharacterEncoding(encoding); // 实验证明这货没起什么作用,所以,不要也行
}
}
}
/**
* 读取encoding参数以及enable参数,enable参数与encoding的功能无关,只是用来启用或者停用这个filter的
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
if (!Charset.isSupported(encoding)) {
encoding = null; // 如果不支持的话,只能选择默认的encoding了,这个filter就不起作用
}
// 读取enable参数
String enableString = filterConfig.getInitParameter("enable");
if (enableString.equalsIgnoreCase("true")) {
this.enable = true;
} else {
this.enable = false;
}
}
}
web.xml
<filter>
<filter-name>charset-encoding</filter-name>
<filter-class>org.foo.filterdemo.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charset-encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------例2:提交到Servlet,Serlvet再设置为request的attribute,再提交到jsp页面--------
AttributeSetterServlet.java
public class AttributeSetterServlet extends HttpServlet {
/**
* Generated serialVersionUID
*/
private static final long serialVersionUID = 8458482445490259121L;
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 在这里也没有调用request.setCharacterEncoding(),
// 你把CharacterEncodingFilter关闭了试试,看看结果有什么
// 不一样
String attr = req.getParameter("attr");
if (attr != null) {
// System.out.println(this + ", " + attr); // @Debug
attr += " -- 这是一个小尾巴";
req.setAttribute("attr", attr);
req.getRequestDispatcher("display_attribute.jsp").forward(req, resp);
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
input_attribute.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="AttributeSetterServlet" method="post">
<!-- 输入中文来测试 -->
请输入要设置的attribute:<input type="text" name="attr">
<input type="submit" value="显示">
</form>
</body>
</html>
display_attribute.jsp
<%@ page language="java" contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>设置的attribute是:<%=request.getAttribute("attr")%></h1>
</body>
</html>
web.xml中添加如下代码:
<servlet>
<servlet-name>attribute-setter-servlet</servlet-name>
<servlet-class>
org.foo.servletdemo.AttributeSetterServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>attribute-setter-servlet</servlet-name>
<url-pattern>/AttributeSetterServlet</url-pattern>
</servlet-mapping>
使用filter解决request.getParameter的中文乱码问题的更多相关文章
- 解决Request中参数中文乱码问题
1.使用配置过滤器的方式解决 在web.xml中增加过滤器: <!--配置解决中文乱码的过滤器--> <filter> <filter-name>character ...
- tomcat7解决jsp参数传递的中文乱码问题
解决jsp参数传递的中文乱码问题 制作人:全心全意 在jsp页面中,通过参数传递传递中文时,在显示参数值时中文内容变成了乱码.这是因为请求参数的文字编码方式与页面中的不一致造成的,所有的request ...
- SpringMVC学习系列-后记 解决GET请求时中文乱码的问题
SpringMVC学习系列-后记 解决GET请求时中文乱码的问题 之前项目中的web.xml中的编码设置: <filter> <filter-name>CharacterEnc ...
- 解决gitk显示文件内容中文乱码
解决gitk显示文件内容中文乱码 1.git config 命令 设置git gui的界面编码 git config --global gui.encoding utf-8 2.修改配置文件 在~\e ...
- 解决PLSQL Developer 插入中文 乱码问题(转)
原文地址:解决PLSQL Developer 插入中文 乱码问题 PLSQL Developer 插入中文 乱码问题,如图 这个是由于oracle服务器端字符编码 和 Oracle 客户端 字 ...
- 解决windows下vim中文乱码
解决windows下vim中文乱码 windows安装了vim8,也就是gvim后,打开带有中文的文档,显示中文是乱码. 毕竟有许多文档我是用utf-8编码的,所以解决的办法是设置一下编码为utf-8 ...
- fedora23解决gedit和vim中文乱码的问题
fedora23解决gedit和vim中文乱码的问题 a, an, the这些不定/定 冠词并不是在所有的名词 前面都要加. 只有在语义上需要时,才加. 名词的单数/复数 前面不加 冠词的 例子多的是 ...
- 解决 SecureCRT 和 SecureFX 中文乱码
引言 最近老是有小伙伴给我发消息说,下载的 SecureCRT 和 SecureFX 安装打开后连接了自己的服务器或虚拟机后会出现中文乱码,每次都要给一一回复,我倒没事,主要是有时候因为工作的原因,所 ...
- request、response 中文乱码问题与解决方式
request乱码指的是:浏览器向服务器发送的请求参数中包含中文字符,服务器获取到的请求参数的值是乱码: response乱码指的是:服务器向浏览器发送的数据包含中文字符,浏览器中显示的是乱码: ...
随机推荐
- nmap速查表v1.0
基本语法: #nmap [扫描方式] [命令选项] {目标} 扫描目标格式: IPv4 地址: 192.168.1.1IPv6 地址:AABB:CCDD::FF%eth0主机名:www.targe ...
- [置顶] 一个Web报表项目的性能分析和优化实践(一):小试牛刀,统一显示SQL语句执行时间
最近,在开发和优化一个报表型的Web项目,底层是Hibernate和MySQL. 当报表数据量大的时候,一个图表要花4秒以上的时间. 以下是我的分析和体会. 1.我首先需要知道哪些函数执行了多少时间 ...
- ElementUI表单验证使用
1.设计校验方式: 我们表单验证的rules一般封装一个单独的js文件,比如我之前写的这个博客: ElementUI使用问题记录:设置路由+iconfont图标+自定义表单验证 可以修改下:公共的校验 ...
- HTML5 input file控件使用accept过滤限制的文件类型以及在谷歌下打开很慢的问题
在文件上传控件(input type='file')中,可以通过accept属性实现对文件类型的过滤. 一.相关代码: 下面给出对应的文件上传控件代码: <input type="fi ...
- Packagist / Composer 中国全量镜像
用法: 有两种方式启用本镜像服务: 将配置信息添加到 Composer 的配置文件 config.json 中(系统全局配置).见“例1 (推荐方式)” 将配置信息添加到单个项目的 composer. ...
- PHP登入网站抓取并且抓取数据
有时候需要登入网站,然后去抓取一些有用的信息,人工做的话,太累了.有的人可以很快的做到登入,但是需要在登入后再去访问其他页面始终都访问不了,因为他们没有带Cookie进去而被当做是两次会话.下面看看代 ...
- Linq 使用注意
今天在使用Linq的时候,查询很慢.仔细查看在知道,在in的后面跟的是个方法,这个方法返回一个集合. Linq只是语法糖,内部进行了封装.内部的实现也是遍历集合,找到满足条件的元素. 在in的后面使用 ...
- C#应用视频教程2.4 OPENGL虚拟仿真介绍
这一部分我们首先实现视图控制(包括了平移/旋转/缩放),前面我们已经讲过,通过lookat一个函数,或者通过translate+rotate两个函数,都能实现视图的控制(两个函数的方式比较简单,但是通 ...
- C#基础视频教程7.4 如何编写简单游戏
接下来我们实现整个的游戏流程,当点击开始游戏,则需要三个事情开始运行 1 小鸟初始化并往下掉(当然还是可以用按钮让他飞一下) 2 每隔一定时间从左侧产生一个管子(宽度和高度随机,产生周期2000ms左 ...
- Linux系统攻略 用UUID在Fstab中挂载分区
Fstab 文件大家都很熟悉,Linux 在启动的时候通过 fstab 中的信息挂载各个分区,一个典型的分区条目就像这样: /dev/sdb5 /mnt/usb vfat utf8,umask=0 0 ...