Jsp coding problems

Source: Internet
Author: User

The biggest headache for JSP development using Tomcat is the Chinese Garbled text. To sum up the reasons for Tomcat Garbled text, you must understand the following:

 

1. Tomcat generally uses the ISO-8859-1 as the character encoding method by default. So, unless you use request. setCharacterEncoding ("encoding method") in the Servlet; specifies a special encoding method, Tomcat uses the ISO-8859-1 encoding method by default.

 

2. pageEncoding and charset on the JSP page have different meanings. PageEncoding refers to the page encoding format (please remember !, Very important ~, Regardless of the pageEncoding method in your JSP. If Chinese characters are garbled, the reason is not that the encoding method specified by pageEncoding is incorrect, but that the charset attribute is incorrect. Charset is used to display characters on JSP pages in encoding mode. PageEncoding is only responsible for the page encoding format, and then the Java Virtual Machine is responsible for converting the code specified by pageEncoding to the Unicode encoded bytecode file. (That is, whatever method you specify for pageEncoding, it is eventually converted to Unicode encoding .) In addition, please note that if the Chinese characters in the user requests submitted from the client are also encoded using pageEncoding. That is to say, in Servlet request. getParameter ("parameter"); Tomcat reads data by ISO-8859-1 by default, but the actual character encoding method should be the method specified by pageEncoding on the JSP page. Unless the user adds request. setCharacterEncoding ("encoding method ");

 

3. Understand the relationship between pageEncoding and charset. Generally, if the pageEncoding method is specified on the page, for example, <% @ page language = "java" import = "java. util. * "pageEncoding =" GBK "%> If charset is not specified, the page displays characters by default in charset = ISO-8859-1 encoding mode, encode the page according to pageEncoding = "GBK. If the pageEncoding method is not specified in your JSP page, but charset is described, for example:

<% @ PagecontentType = "text/html; charset = UTF-8" %> in this case, JSP performs page encoding according to pageEncoding = "UTF-8" by default, the character set is displayed according to charset = UTF-8.

 

4. Use request. setCharacterEncoding ("encoding method"). Note: request. setCharacterEncoding () only applies to the POST submission method, and garbled characters may occur for GET submission. To solve the problem of Chinese garbled characters submitted by GET, you can go to <Connector port = "8888" protocol = "HTTP/1.1" in Server. xml"
 
ConnectionTimeout = "20000"
RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>

Add: URIEncoding = "UTF-8" useBodyEncodingForURI = "true"

In addition, the request. setCharacterEncoding () statement must be called before the first parameter to be read. Otherwise it will not work !!

 

Let's look at a small example:

On the index. jsp page, specify the pageEncoding mode:

------------------------------------------------------------------

<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>

<Html>
<Head>
<Basehref = "<% = basePath %>">
 
<Title> My JSP 'index. jsp 'startingpage </title>
 
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is mypage">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->

</Head>

<Body>
<Form action = servlet/Loginmethod = post>
Username: <input type = text name = "username" size = 20> <br>
Password: <input type = "password" name = "password" size = 20> <br>
<Input type = submitname = "submit">
</Form>
</Body>
</Html>
-------------------------------------------------------------

In the Login. java Servlet Source file:

Public void doPost (HttpServletRequest request, HttpServletResponseresponse)
Throws ServletException, IOException {

 
PrintWriter out = response. getWriter ();
Out. print ("<H> welcome you comehere! </H> ");
Out. print ("<br> ");
Out. println (request. getParameter ("username "));
Out. print ("<br> ");
Out. println (request. getParameter ("password "));
Out. print ("<br> ");
Out. flush ();
System. out. println (response. getCharacterEncoding ());
Out. close ();
}

When you enter the Chinese user name and call the Login Servlet, the results will display Chinese characters normally. It seems strange to you to see it here.

Because the request is not used in LoginServlet. setCharacterEncoding ("encoding method"); specifies the encoding method of the feature, that is to say, Tomcat is the default way to use the ISO-8859-1, how can the normal display of Chinese? The problem lies in IE browser. Please note that! The default Character Set of Chinese IE browser is charset = GBK. Please note that response is not specified in the above LoginServlet code. setContentType ("text/html; charset = GBK"); such a statement, that is, the IE browser considers that since the user does not specify a character set, the default character set is used, that is, charset = GBK is used by default,

So you see the Chinese display, is the IE browser silently help you convert, from ISO-8859-1 to GBK conversion, that is to say, IE browser to help you do this sentence:

Out. println (newString (request. getParameter ("username"). getBytes ("ISO-8859-1"), "gbk "));

Please note that if you add response to the Login Servlet Source code. setContentType ("text/html; charset = GBK"); then you can draw a snake, but it does not work well, because ie considers that you have specified a specific character set, and is charset = GBK mode, so do not need to convert from ISO-8859-1 to GBK, because IE browser that the character in your page should be GBK encoding, but in fact it is not, is ISO-8859-1, so the display of Chinese garbled.

 

Next, let's change the small example above: the index. jsp page only specifies the charset method:

<% @ PagecontentType = "text/html; charset = UTF-8" %>

<Html>
<Head>
<Basehref = "<% = basePath %>">
 
<Title> My JSP 'index. jsp 'startingpage </title>
 
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is mypage">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->

</Head>

<Body>
<Form action = servlet/Loginmethod = post>
Username: <input type = text name = "username" size = 20> <br>
Password: <input type = "password" name = "password" size = 20> <br>
<Input type = submitname = "submit">
</Form>
</Body>
</Html>
-------------------------------------------------------

In the Login. java Servlet Source file:

Public void doPost (HttpServletRequest request, HttpServletResponseresponse)
Throws ServletException, IOException {

Response. setContentType ("text/html; charset = UTF-8 ");

// Response. setContentType ("text/html; charset = GBK"); // Chinese characters can be displayed normally

Request. setCharacterEncoding ("UTF-8 ");
PrintWriter out = response. getWriter ();
Out. print ("<H> welcome you comehere! </H> ");
Out. print ("<br> ");
Out. println (request. getParameter ("username "));
Out. print ("<br> ");
Out. println (request. getParameter ("password "));
Out. print ("<br> ");
Out. flush ();
System. out. println (response. getCharacterEncoding ());
Out. close ();
}

 

Because in index. jsp

<% @ PagecontentType = "text/html; charset = UTF-8" %> the default pageEncoding = UTF-8

Therefore, you must add:

Response. setContentType ("text/html; charset = UTF-8 ");

Request. setCharacterEncoding ("UTF-8 ");
These two sentences can display Chinese normally!
Author: mark_qi

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.