Struts2之Web应用中文乱码问题的6种解决方法
在web应用中通常用下面几种方法解决中文问题:
1.HTML页面中文问题
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>Insert title here</title></head><body></body></html>
<%@ page language="java" contentType="text/html; charset=gb2312" %>
response.setCharacterEncoding("gb2312");
<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>
<%@ 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"));
String url = "jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gb2312";