在web应用中通常用下面几种方法解决中文问题:
1.HTML页面中文问题
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
2.JSP页面中文问题
<%@ page language="java" contentType="text/html; charset=gb2312" %>
3.Servlet响应结果中文问题
response.setCharacterEncoding("gb2312");
4.解决页面或者组件间数据传输的中文问题
<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>
5.利用java.net.URLDecoder类使用get方法传递参数
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% session.setAttribute("wholename", "系统管理员"); %>
<html>
<head>
<title>模拟登录界面</title>
<script type="text/javascript">
function login() {
document.getElementById("form").submit();
}
function dogetMethod(url) {
//url编码前: leavesp?work=部门主管审批
url = encodeURI(url);
}
</script>
</head>
<body>
<form id="form" method="post" action="http://127.0.0.1/demo/login">
<input type="hidden" name="userName" value="<%=java.net.URLEncoder.encode(session.getAttribute("wholename").toString(),"gbk")%>" />
<input type="button" value="登录" onclick="login()"/>
</form>
</body>
</html>
在后台解码:
setUserName(java.net.URLDecoder.decode(userName,"gbk"));
6.解决MySql数据库中文问题(主要在JDBC的驱动URL)
String url = "jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gb2312";