JSP page display garbled problem solution

Source: Internet
Author: User
Tags getmessage return
js| Solution | problem | show | page

One, JSP page display garbled

Second, the form submitted in Chinese when there are garbled

Third, the database connection appears garbled

Everyone in the development process of JSP, often the problem of Chinese garbled, may be one to haunt you, I am now in the development of JSP encountered in Chinese garbled problems and solutions to write for your reference.

One, JSP page display garbled

The following display page (display.jsp) will appear garbled:

       
        
         
        

For different Web servers and different JDK versions, the processing results are not the same. Reason: The server uses different encoding methods and browsers to different characters display results. Workaround: Specify the Encoding (GB2312) in the JSP page, that is, the first line of the page plus:

       
        
         
        English Code <%@ page contenttype= "text/html; charset=gb2312 "%>
       
        

You can eliminate the garbled. The complete page is as follows:

       
        
         
        <%@ page contenttype= "text/html; charset=gb2312 "%> 

Second, the form submitted in Chinese when there are garbled

Below is a submission page (submit.jsp) with the following code:

       
        
         
        

The following is the Process page (process.jsp) code:

       
        
         
        <%@ page contenttype= "text/html; charset=gb2312 "%> 

If the submit.jsp submit English characters can be displayed correctly, if the submission of Chinese will appear garbled. Reason: Browsers use UTF-8 encoding to send requests by default, while UTF-8 and GB2312 encodings are different when they represent characters, which can result in unrecognized characters. Solution: Through the request.secharacterencoding ("gb2312") to the request for unified coding, the realization of the normal Chinese display. The modified process.jsp code is as follows:

       
        
         
        <%@ page contenttype= "text/html; charset=gb2312 "%> <% request.secharacterencoding (" gb2312 "); %> 

Third, the database connection appears garbled

As long as the Chinese-related places are all garbled, the solution: in the database URL of database add USEUNICODE=TRUE&CHARACTERENCODING=GBK OK.

Four, the database display garbled

In mysql4.1.0, varchar type, text type will appear in Chinese garbled, for the varchar type to set it as binary attribute can solve the Chinese problem, for text type to use a code conversion class to deal with, realize as follows:

       
        
         
        public string ISO2GB (string qs) {try{if (qs = null) return ' null '; else {return new String (Qs.getbytes ("iso-8859-1"), " gb2312 "); The catch (Exception e) {System.err.println ("ISO2GB Error:" +e.getmessage ());} return "NULL"; public string Gb2iso (string qs) {try {if (QS = null) return ' null '; else {return new String (Qs.getbytes ("gb2312"), "I So-8859-1 "); The catch (Exception e) {System.err.println ("Gb2iso Error:" +e.getmessage ());} return "NULL"; }
       
        

When the character is stored in the database, use the Gb2iso () function to remove the characters from the database, and then use the ISO2GB () function.



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.