javascript passes Chinese parameters to jsp pages via url resulting in a garbled code solution

  • 2020-06-01 10:50:51
  • OfStack

2013-1-16 10:35:49 org.apache.tomcat.util.http.Parameters processParameters
Warning: Parameters: Character id with value '% u '8en with value '% u5355' has been value quoted may corrupted due the failed decoding. Use debug level logging see the original, non-corrupted values
Enter Servlet Get Method..
OK:
2013-1-16 10:35:49 org.apache.catalina.core.StandardWrapperValve invoke
Serious: 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)

Solution: encodes the Chinese parameters twice before passing them, and decodes the Chinese parameters once after the jsp page gets the parameters, so the Chinese parameters will not become scrambled!

Reference example:
 
<%@ 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");// Get Chinese parameters directly  
} 
try{ 
if(request.getParameter("param1")!=null){ 
str1=URLDecoder.decode(request.getParameter("param1"),"utf-8");// Decode the Chinese parameters  
} 
}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=" hello "; 
function test0(){ 
window.location="Test.jsp?param0="+str;// Pass Chinese parameters directly  
} 
function test1(){ 
window.location="Test.jsp?param1="+encodeURI(encodeURI(str));// The Chinese parameters are double-coded and then passed  
} 
</script> 
</head> 
<body> 
<input value=<%=str0 %>> 
<input type="button" value=" The statement " onclick="test0()"><br> 
<input value=<%=str1 %>> 
<input type="button" value=" normal " onclick="test1()"> 
</body> 
</html> 

Related articles: