Java中文乱码解决方案
1.中文乱码解决方案,确保每个文件的默认编码是UTF-8
加入 URIEncoding="UTF-8"
代码中的设置
1>在servlet中加入
//1.设置response的编码为utf-8
response.setCharacterEncoding("utf-8");
//2.通知浏览器,以UTF-8的编码打开
response.setContentType("text/html;charset=UTF-8");
2>在应用的地方加入
new String(userName.getBytes("ISO-8859-1"), "gb2312")
3>在JSP文件中加入
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312" %>
4>在web.xml中加入过滤器
<!-- 配置中文乱码过滤 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>