javascript通過url向jsp頁面傳遞中文參數導致亂碼解決方案

來源:互聯網
上載者:User

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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.