1、表单get方法提交参数乱码,由于默认的tomcat按照iso-8859-1对url进行编码所以要进行相应转换:
@首先在接受参数页面或者servlet设置request的编码方式例如:request.setCharacterEncoding("utf-8");
@对接受到的参数改变编码,和上面的characterencoding的编码相同,new String(request.getParameter("username").getBytes("iso-8859-1"),"utf-8");
@修改tomcat的server.xml文件,<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080"protocol="HTTP/1.1" redirectPort="8443" />
@以上方法对ie6好像是如果有多个参数的话,会把参数之间的&符号变为??,不知道怎么解决,求解。
2、表单post方法提交参数乱码,这个很简单,有两种简单的办法;
@首先在接受参数页面或者servlet设置request的编码方式例如:request.setCharacterEncoding("utf- 8");这里的utf-8编码和你传递参数的页面的contentType或者pageEncoding一致。
@编写过滤器,方法略。
3、javascript通过url向jsp页面传递中文参数乱码解决方法(转载)
@解决方法:在传递参数前将中文参数进行两次编码,jsp页面获取参数后对中文参数进行一次解码,中文参数就不会 变为乱码了!
参考例子:
pageEncoding = " UTF-8 " %>
<% @ page import = " java.net.* " %>
<%
String str0 = "" ;
String str1 = "" ;
if (request.getParameter( " param0 " ) != null ){
str0 = request.getParameter( " param0 " ); // 直接获取中文参数
}
try {
if (request.getParameter( " param1 " ) != null ){
str1 = URLDecoder.decode(request.getParameter( " param1 " ), " utf-8 " ); // 对中文参数进行解码
}
} catch (Exception e){
e.printStackTrace();
}
%>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=UTF-8 " >
< script type = " text/javascript " >
var str = " 你好 " ;
function test0(){
window.location = " Test.jsp?param0= " + str; // 直接传递中文参数
}
function test1(){
window.location = " Test.jsp?param1= " + encodeURI(encodeURI(str)); // 对中文参数进行双层编码后再传递
}
</ script >
</ head >
< body >
< input value =<%= str0 %>>
< input type = " button " value = " 乱码 " onclick = " test0() " >< br >
< input value =<%= str1 %>>
< input type = " button " value = " 正常 " onclick = " test1() " >
</ body >
</ html >