javaWeb中乱码问题
1.解决HTML页面中的中文乱码
<META http-equiv=Content-Type content="text/html;charset=utf-8">
2.解决JSP页面中的中文乱码问题
<%@ page contentType="text/html;charset=utf-8" language="java" %>
3.解决Servlet响应结果的中文
response.setCharacterEncoding("utf-8");
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>
</filter>
<filter-mapper>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*<url-pattern>
</filter-mapper>
5.解决Http中get请求方式的乱码
String parameter= request.getParameter("parameter");
if(parameter!=null)
parameter=new String(parameter.getBytes("ISO-8859-1"),"UTF-8");
(tomcat中servser.xml中 添加
<Connector port="8080"
maxThreads="150"
...
disableUploadTimeout="true"
URIEncoding="utf-8"
>)
方式三:(不管server.xml怎么配,都不会有问题)
思路:在页面中两次URL编码,
在Action中自己做一次URL解码。
1.在JS中:
param = encodeURI(param);
param = encodeURI(param);
function show(id){
对id进行一次url编码
id =encodeURI(id)
对id进行二次url编码
id =encodeURI(id)
var url="${pageContext.request.contextPath}/one.action?id="+id;
window.showModalDialog(url,null,"dialogHeight:450px;dialogWidth:600px;resizable:yes");
}
2.在action类中进行解码:
解码 URLDecoder.decode(id,"utf-8");
(编码) java.net.URLEncoder.encode(id,"utf-8");
3.在<s:a>中:
<s:a action="oneAction_delete">
<s:param name="key" value-"%{@java.net.URLEncoder@encode(key,"utf-8")}" />
删除
</s:a>
4.设置ognl表达式中可以调用静态方法
<constant name="struts.ognl.allowStaticMethodAccess" value="true">
</constant>
6.解决Mysql数据库中乱码
jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=utf-8