Javascript uses url to pass Chinese parameters to jsp pages, resulting in garbled code Solution

Source: Internet
Author: User

10:35:49 org. apache. tomcat. util. http. Parameters processParameters
Warning Parameters: Character decoding failed. parameter 'id' with value' % u8BA2 % u5355 'has been ignored. note that the name and value quoted here may have upted due to the failed decoding. use debug level logging to see the original, non-parallel upted values.
Enter Servlet Get Method ..
OK:
10:35:49 org. apache. catalina. core. StandardWrapperValve invoke
Severe: 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: Encode the Chinese parameter twice before passing the parameter. After obtaining the parameter on the jsp page, decode the Chinese parameter once. The Chinese parameter will not become garbled!

Example:Copy codeThe Code is as follows: <% @ 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"); // decodes 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; // directly pass Chinese Parameters
}
Function test1 (){
Window. location = "Test. jsp? Param1 = "+ encodeURI (str); // encode Chinese parameters and pass them again
}
</Script>
</Head>
<Body>
<Input value = <% = str0 %>
<Input type = "button" value = "garbled" onclick = "test0 ()"> <br>
<Input value = <% = str1 %>
<Input type = "button" value = "normal" onclick = "test1 ()">
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.