欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javascript通过url向jsp页面传递中文参数导致乱码解决方案

程序员文章站 2024-02-14 19:40:28
2013-1-16 10:35:49 org.apache.tomcat.util.http.parameters processparameters 警告: parame...
2013-1-16 10:35:49 org.apache.tomcat.util.http.parameters processparameters
警告: parameters: character decoding failed. parameter 'id' with value '%u8ba2%u5355' has been ignored. note that the name and value quoted here may corrupted due to the failed decoding. use debug level logging to see the original, non-corrupted values.
enter servlet get method..
ok:
2013-1-16 10:35:49 org.apache.catalina.core.standardwrappervalve invoke
严重: servlet.service() for servlet suggest threw exception
java.lang.nullpointerexception
at com.ont.demo.suggest.doget(suggest.java:25)
at javax.servlet.http.httpservlet.service(httpservlet.java:617)
at javax.servlet.http.httpservlet.service(httpservlet.java:717)
at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:290)
at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206)
at org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:233)
at org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:191)
at org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:127)
at org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102)
at org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109)
at org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:298)
at org.apache.coyote.http11.http11processor.process(http11processor.java:852)
at org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:588)
at org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:489)
at java.lang.thread.run(thread.java:619)

解决方法:在传递参数前将中文参数进行两次编码,jsp页面获取参数后对中文参数进行一次解码,中文参数就不会变为乱码了!

参考例子
复制代码 代码如下:

<%@ page language="java" contenttype="text/html; charset=utf-8"
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>