J2EE WEB-Tomcat5.5.9 Chinese problem solution

Source: Internet
Author: User

1. html

Whether it is an independent html, or generated by other programs, such as Servlet, note that meta tags must be added between the final html and to specify the encoding of characters entered in html, such:

 

<Head> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <title> test GET & POST-Send </title>

2. jsp and servlet

First, the program output (such as response. writeln (String s) and accept data from the client (such as request. getParameter (String sname) encoding problem, we can use the File filter function, the specific jsp/servlet container or the function settings provided by the server, such as in Tomcat5.5.9 can be in webapps/yourAppDirectory/WEB-INF/web. the settings in xml are as follows:

      <filter>   <filter-name>SetCharsetEncodingFilter</filter-name>   <display-name>SetCharsetEncodingFilter</display-name>   <description>Set CharsetEncoding Filter</description>   <filter-class>com.gg.comm.web.SetCharsetEncodingFilter</filter-class>   <init-param>   <param-name>encoding</param-name>   <param-value>gb2312</param-value>   </init-param>   </filter>   <filter-mapping>   <filter-name>SetCharsetEncodingFilter</filter-name>   <url-pattern>/*</url-pattern>   </filter-mapping>   

The SetCharsetEncodingFilter Class is the filter Class used to set the request and reponse character encoding. The setting statement is as follows:

     request.setCharacterEncoding(targetEncoding);   response.setContentType("text/html");   response.setCharacterEncoding(targetEncoding);   

In addition, to solve the problem of garbled characters when passing parameters through get (url with parameters), we also need to set the encoding required for url passing parameters, in Tomcat5.5.9, you can go to $ {Tomcat_home} confserver. in xml, the settings are as follows:

     <!--   URIEncoding="GBK":Force GET method String(Chinese) can be transferd properly by http:uri      note:Tomcat only support GBK specification,so not set charset gb2312   -->   <Connector URIEncoding="GBK" port="80" redirectPort="8443" maxSpareThreads="75"   maxThreads="150" minSpareThreads="25">   </Connector>   

Finally, in order to solve the problem of jsp garbled code, we also need to do the following, that is, add the following command to the left and right jsp headers:

   <% @ Page contentType = "text/html; charset = gb2312" language = "java" %> or <% @ page pageEncoding = "gb2312" %>

3. JDBC and database

You can easily solve the garbled characters in writing and reading database data by using the following methods:

The JAVA program is processed in the method we specify.

Change the encoding format supported by the database to GBK or GB2312 by default.

At this point, Chinese problems can be solved for WEB applications. Of course, the above method is based on the unified encoding principle and the file conversion relationship (file-> class-> load-> execute or transfered or response or request) in the WEB mode.

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.