Jsp common garbled Problem Solution

Source: Internet
Author: User

1, form get method to submit parameters garbled, because the default tomcat according to the iso-8859-1 for url encoding so the corresponding conversion:
@ First, set the request Encoding Method on the acceptance parameter page or servlet, for example: request. setCharacterEncoding ("UTF-8 ");

@ The parameter changed encoding of the connection, which is the same as the characterencoding code above, new String (request. getParameter ("username"). getBytes ("iso-8859-1"), "UTF-8 ");

@ Modify the tomcat server. xml file, <Connector URIEncoding = "UTF-8" connectionTimeout = "20000" port = "8080" protocol = "HTTP/1.1" redirectPort = "8443"/>

@ The above method seems to have multiple parameters for ie6, The & symbol between parameters will be changed ??, I don't know how to solve it.

 

 

2. The parameters submitted by form post are garbled. There are two simple methods;

@ First, set the request Encoding Method on the acceptance parameter page or servlet, for example, request. setCharacterEncoding ("utf-8"); the UTF-8 encoding here is consistent with the contentType or pageEncoding of the page on which you pass the parameters.

@ Compile the filter. The method is omitted.

3. javascript uses url to pass Chinese parameter garbled characters to jsp pages (reproduced)

@ 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:
<% @ 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>

The author is like a play"
 

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.