How to solve the problem of garbled JSP page display

Source: Internet
Author: User

I. garbled characters on the JSP page

The following JSP page (display. jsp) is garbled:

 
 
  1. <Html>
  2. <Head>
  3. <Title>JSP Chinese Processing</Title>
  4. <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
  5. </Head>
  6. <Body>
  7. <%
  8. Out. print ("JSP Chinese processing ");
  9. %>
  10. </Body>
  11. </Html> 

Different WEB servers and JDK versions have different processing results. Cause: the encoding method used by the server is different from that used by the browser to display different characters. Solution: Specify the encoding method (gb2312) on the JSP page, that is, add the following code to the first line of the page:

 
 
  1. <%@ page contentType="text/html; charset=gb2312"%> 

The garbled characters can be eliminated. The complete page is as follows:

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%>
  2. <Html>
  3. <Head>
  4. <Title>JSP Chinese Processing</Title>
  5. <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
  6. </Head>
  7.  
  8. <Body>
  9. <%
  10. Out. print ("JSP Chinese processing ");
  11. %>
  12. </Body>
  13. </Html> 

2. garbled characters appear when the form is submitted to Chinese

The following is a submission page (submit. jsp). The Code is as follows:

 
 
  1. <Html>
  2. <Head>
  3. <Title>JSP Chinese Processing</Title>
  4. <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
  5. </Head>
  6. <Body>
  7. <Form Name="Form1" Method="Post" Action="Process. jsp">
  8. <Div Align="Center">
  9. <Input Type="Text" Name="Name">
  10. <Input Type="Submit" Name="Submit" Value="Submit">
  11. </Div>
  12. </Form>
  13. </Body>
  14. </Html> 

The following is the process. jsp code:

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%>
  2. <Html>
  3. <Head>
  4. <Title>JSP Chinese Processing</Title>
  5. <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
  6. </Head>
  7. <Body>
  8. <% = Request. getParameter ("name") %>
  9. </Body>
  10. </Html> 

If the English characters submitted by submit. jsp are correctly displayed, garbled characters will appear when you submit Chinese characters. Cause: by default, the browser uses the UTF-8 encoding method to send requests, while the UTF-8 and GB2312 encoding method to indicate the character is not the same, so there is an unrecognized character. Solution: requests are uniformly encoded using request. seCharacterEncoding ("gb2312") to display Chinese characters normally. The modified process. jsp code is as follows:

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%>
  2. <%
  3. Request. seCharacterEncoding ("gb2312 ");
  4. %>
  5. <Html>
  6. <Head>
  7. <Title>JSP Chinese Processing</Title>
  8. <Meta Http-equiv="Content-Type" Content="Text/html; charset = gb2312">
  9. </Head>
  10.  
  11. <Body>
  12. <% = Request. getParameter ("name") %>
  13. </Body>
  14. </Html> 

3. garbled database connection

As long as all Chinese characters are garbled, solution: Add useUnicode = true & characterEncoding = GBK to the Database URL.

4. garbled display of databases

In mysql4.1.0, Chinese garbled characters are displayed for the varchar and text types. Setting the varchar type as the binary Attribute can solve Chinese problems, for the text type, an encoding and conversion class must be used for processing. The implementation is as follows:

 
 
  1. public String iso2gb(String qs)   
  2. {   
  3. try{   
  4. if (qs == null) return "NULL";   
  5. else   
  6. {   
  7. return new String(qs.getBytes("iso-8859-1"),"gb2312");   
  8. }   
  9. }   
  10. catch(Exception e){   
  11. System.err.println("iso2gb error:"+e.getMessage());   
  12. }   
  13. return "NULL";   
  14. }   
  15. public String gb2iso(String qs)   
  16. {   
  17. try   
  18. {   
  19. if (qs == null) return "NULL";   
  20. else {   
  21. return new String(qs.getBytes("gb2312"),"iso-8859-1"); }   
  22. }   
  23. catch(Exception e){ System.err.println("gb2iso error:"+e.getMessage());}   
  24. return "NULL";   

The above is the problem that Chinese characters are garbled frequently on JSP pages during development, which may affect you as soon as possible, I am writing out the Chinese garbled characters I encountered during JSP development and the solutions for your reference.

  1. Notes for connecting JSP to ORACLE databases
  2. Configuration of tomcat SQL Server2000 Database Connection Pool in JSP
  3. Differences between Servlets and JSP
  4. Differences between JSP include commands and include actions
  5. Solutions to JSP output of excel documents and Chinese garbled characters

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.