在开发Java/Java Web Application过程中,往往会出现乱码问题,而且有的时候真会弄得人很烦,浪费太多的时间。

记得之前看过一篇帖子,详细解释了Encoding/Decoding过程,不过时间久远已经淡忘。。。

其实针对这种乱码问题,记录问题+查阅解决方案是比较好的解决办法。

问题1:JSP页面中的EL表达式输出出现乱码,如

由book.jsp页面通过href转向modifyBook.jsp页面

book.jsp

<c:forEach var="book" items="${allBooks}">
<tr>
<td>${book.bookId}</td>
<td>${book.bookName}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>${book.publisher}</td>
<td><a href="deleteBook?bookid=${book.bookId}">删除</a></td>
<td><a href="modifyBook?bookid=${book.bookId}&bookname=${book.bookName}&author=${book.author}&price=${book.price}&publisher=${book.publisher}">修改</a></td>
</tr>
</c:forEach>

modifyBook.jsp

<form action="${pageContext.request.contextPath}/modifyBook" method="post">
<table>
<tr>
<td><input type="text" name="bookid" value="${param.bookid}" readonly/></td>
<td><input type="text" name="book_name" value="${param.bookname}"/></td>
<td><input type="text" name="author" value="${param.author}"/></td>
<td><input type="text" name="price" value="${param.price}"/></td>
<td><input type="text" name="publisher" value="${param.publisher}"/></td>
</tr>
</table>
<br/>
<input type="submit" name="提交" value="Submit"/>
</form>

EL表达式${param.publisher}的输出乱码。

解决方案:

1. 首先检查各个JSP页面的编码方式,确认均为contentType="text/html;charset=UTF-8";

2. 检查跳转后的url地址如下:http://localhost:8080/basicmvc/modifyBook?bookid=00003&bookname=Java%20EE&author=wang&price=30.0&publisher=人民邮电,不是乱码。

3. 由2说明${param.publisher}的源没有问题,就是输出编码的问题。折腾半天,想起tomcat的编码配置URIEncoding="UTF-8"。解决!

问题2:接上一个问题,中文数据展示没有问题了。但紧接着更新数据库后,数据库中出现中文乱码??。

既然数据源没有问题,那就是数据库这边的问题了,但是数据库创建和表的创建都是UTF-8的方式。再查看Java代码,应该是PreparedStatement使用setString出现中文乱码。查询后,得知数据库连接中需要设置编码方式,恍然大悟(之前解决过类似问题,只是给忘了。。。),如下:

jdbc:mysql://localhost:3306/crud?useUnicode=true&characterEncoding=UTF-8

问题3:在另一环境中,解决了问题1和问题2,但还是出现了乱码问题。

唯一区别就是Filter类的使用, Filter类似于Servlet,在web.xml中配置。

解决方案:(代码胜千言)

EncodingFilter:

public class EncodingFilter implements Filter {

    //配置中默认的字符编码
protected String encoding = null;
protected FilterConfig filterConfig;
//当没有指定默认编码时是否允许跳过过滤
protected boolean ignore = true; @Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
System.out.println(this.encoding);
String value = filterConfig.getInitParameter("ignore");
if (value == null){
this.ignore = true;
} else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
} else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
} else {
this.ignore = false;
}
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
HttpServletResponse httpServletResponse=(HttpServletResponse)response;
//Conditionally select and set the character encoding to be used
if(ignore || httpServletRequest.getCharacterEncoding() == null){
String coding = selectEncoding(httpServletRequest);
if(coding != null){
httpServletRequest.setCharacterEncoding(coding);
httpServletResponse.setCharacterEncoding(coding);
}
}
//将控制器传向下一个filter
chain.doFilter(httpServletRequest, httpServletResponse);
} @Override
public void destroy() {
this.encoding = null;
this.filterConfig = null;
} protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}

web.xml

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.chris.web.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

问题3:待续

Java/Java Web中乱码解决汇总的更多相关文章

  1. Java或web中解决所有路径问题

    Java开发中使用的路径,分为两种:绝对路径和相对路径.归根结底,Java本质上只能使用绝对路径来寻找资源.所有的相对路径寻找资源的方法,都不过是一些便利方法.不过是API在底层帮助我们构建了绝对路径 ...

  2. Eclipse中文乱码解决汇总(应该比较全):

    Eclipse中文乱码解决汇总(应该比较全,欢迎补充): 方法一: 把GBK改成utf-8. 方法二: Window->preference->general->content ty ...

  3. java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别

    这里先写几个大家容易搞混的编码设置代码: 在jsp代码中的头部往往有这两行代码 pageEncoding是jsp文件本身的编码contentType的charset是指服务器发送给客户端时的内容编码J ...

  4. ajax乱码解决汇总

    ajax乱码解决总结第一,javascript沿用java的字符处理方式,内部是使用unicode来处理所有字符的,第二,utf-8是每个汉字(unicode字符)用3个字节来存储.第三,用utf-8 ...

  5. java web中乱码的种类和一些解决方式

    在java web课堂测试中遇到了一些乱码问题 ,从百度上找到了许多种解决方法和乱码的种类,在这里总结一下. 一.文件出现乱码 [右击文件]->[Properties]->[Resourc ...

  6. Java Web中乱码问题

    response.setContentType("text/html;charset=UTF-8"); 用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式.什么编码读取这个 ...

  7. Java的各种中文乱码解决方法

    一.Servlet输出乱码 1. 用servlet.getOutStream字节流输出中文,假设要输出的是String str ="钓鱼岛是中国的,无耻才是日本的". 1.1 若是 ...

  8. java 页面传输中文乱码解决方式

    post 中文乱码解决方案 接受数据的时候设置 request.setCharacterEncoding("utf-8");//编码必须和页面编码一致 页面设置 <%@pag ...

  9. java文件下载以及中文乱码解决

    在客户端下载文件时替换下载文件的名称,但是当名称是中文时浏览器会出现乱码,解决代码如下: public org.springframework.http.ResponseEntity<Input ...

随机推荐

  1. excute和query

    query(update goods set is_delete=1 where goods_id=13)总是出错??为什么, excute(update goods set is_delete=1 ...

  2. Java 中判断两个对象是否相等

    由于每次实例化一个对象时,系统会分配一块内存地址给这个对象,而系统默认是根据内存地址来检测是否是同一个对象,所以就算是同一个类里实例化出来的对象它们也不会相等. public class Transp ...

  3. mysql连接查询经典小例题

    mysql连接查询: Mysql连接查询支持多表连接 对同一张表可以重复连接多次(别名在多次连接同一张表时很重要) 例题1: 下面有2张表 teams表 比赛结果表:result 问题: 得出一张表: ...

  4. PHP+ajax聊天室源码!支持长轮循跟定时请求两种

      var lastID = "1";//声明上次取回的消息的ID var isposted = false; var mGetTime;//设置setTimeout的返回值 // ...

  5. 实战Django:简易博客Part1

    舍得学习新技能的时候,通常不喜欢傻读书--捧着一本阐述该项技能的书籍,然后傻看,一路看下来,脑子里塞满了新的概念.知识点,头是越来越大,但技能却几乎没掌握半分. 多年来,舍得养成了用做实例来学习新技能 ...

  6. windows下python+flask环境配置详细图文教程

    本帖是本人在安装配置python和flask环境时所用到的资源下载及相关的教程进行了整理罗列,来方便后面的人员,省去搜索的时间.如果你在安装配置是存在问题可留言给我. 首先罗列一下python+fla ...

  7. Activity(一)

    一个应用程序中至少包含一个Activity,Activity启动流程:当启动一个应用程序时,android操作系统会访问该应用程序的AndroidManifest.xml文件(该文件中说明了应用程序使 ...

  8. Cygwin ssh服务配置 (SecureCRT连接Cygwin配置)

    1.运行ssh-host-config 这里需要注意的是标红部分,输入的用户名或密码要符合计算机的用户名或密码策略(尤其是公司有权限限制的电脑). $ ssh-host-config *** Quer ...

  9. "奇葩家园“之 asyncTask 与 url 下载篇

    asyncTask 是android提供的一个轻量级的异步处理的类,有3个泛型参数,params,progress,result params: 启动任务执行的时候传入的参数比如请求的 url 地址 ...

  10. golang初试:坑爷的

    用Golang与perl脚本比较, 初想至多差一倍吧...结果可不是一般的坑爹, 简直就是坑爷了. Perl脚本 #!/bin/bash source /etc/profile; function e ...