Java Servlet's method for handling garbled characters, assumervlet

Source: Internet
Author: User

Java Servlet's method for handling garbled characters, assumervlet

When I deploy a webservices program today, a garbled error occurs in the servlet that obtains data from the page. In the servlet, I have configured the request. setCharacterEncoding ("GB2312"); this text is added to the code, but it is still messy.
Find information online, as follows: JAVA is Unicode encoding, you first convert to ISO8859-1, and then convert to GBK or GB2312.

Java code

Copy codeThe Code is as follows:
Request. setCharacterEncoding ("ISO8859-1 ");
Ad = request. getParameter ("name ");
Byte [] temp3 = ad. getBytes ("GBK ");
String str = new String (temp3 );

This is Chinese!
Another simpler method is to set the encoding and conversion directly on the server. Tomcat 5.0.28 is used.
Find the SERVER. XML file in the. \ Tomcat 5.0 \ conf directory and open it with a WordPad. The settings are as follows:
You can add an attribute, URIEncoding = "GBK"
After the test is passed, the request is completed. what is the role of setCharacterEncoding ("GB2312")? If it cannot solve the problem of chaos, what is the role of setCharacterEncoding? Sort out the information as follows:
(1) Chinese characters are displayed on the JSP page, but garbled characters are displayed:
The solution is in the JSP page encoding, because Jsp into Java file encoding problem, some of the default server is ISO-8859-1, if a JSP directly entered the Chinese, jsp treats it as a ISO8859-1 and there is certainly a problem, which we can confirm by viewing the Java intermediate file generated by Jasper

(2) garbled characters occur when the Request object is used to obtain the Chinese character code submitted by the customer:
The solution is to configure a filter, that is, a Servelet filter. The Code is as follows:
Copy codeThe Code is as follows:
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

Request. setCharacterEncoding ("GBK ");

// Transfer control to the next Filter
Chain. doFilter (request, response );
}

Configure web. xml
Copy codeThe Code is as follows:
<Filter> </filter>
<Filter-name> </filter-name> Set Character Encoding
<Filter-class> </filter-class> SetCharacterEncodingFilter

<Filter-mapping> </filter-mapping>
<Filter-name> </filter-name> Set Character Encoding
<Url-pattern> </url-pattern> /*

If you still see this situation, you can see if you have encountered the fourth situation and whether the data submitted by your Form is submitted using get, in general, there is no problem in submitting a post statement. If so, let's take a look at the solution in step 4.

The following code processes the information containing Chinese characters:
Copy codeThe Code is as follows:
Public String toUni (String gbStr ){
String uniStr = "";
If (gbStr = null ){
GbStr = "";
}
Try {
Byte [] tempByte = gbStr. getBytes ("GB2312 ");
UniStr = new String (tempByte, "ISO8859_1 ");
} Catch (Exception ex ){
}
Return uniStr;
}
}

You can also directly convert it, first you encode the obtained string with the ISO-8859-1, and then store the encoding into a byte array, then convert the array into a string object. For example:
String str = request. getParameter ("girl ");
Byte B [] = str. getBytes ("ISO-8859-1 ");
Str = new String (B );
Any submitted information can be correctly displayed after the preceding conversion.
(3) The Formget request is used on the server. when getParameter ("name") is returned, garbled characters are returned. It is useless to set Filter Based on tomcat or request. setCharacterEncoding ("GBK"); also, the problem lies in the method for passing the processing parameters. If doGet (HttpServletRequest request, HttpServletResponse response) is used in the servlet) if the method is used for processing, the preceding statements are written as follows:
Copy codeThe Code is as follows:
Request. setCharacterEncoding ("GBK ");
Response. setContentType ("text/html; charset = GBK ");

It does not work either. The returned Chinese characters are still garbled !!! If you change this function to doPost (HttpServletRequest request, HttpServletResponse response), everything will be OK.
Similarly, when two JSP pages are used to process form input, the reason why Chinese characters can be displayed is that the post method is used for transmission, and the get method cannot be used.
It can be seen that you should pay attention to using the doGet () method in servlet or the get method in JSP. After all, it involves passing parameter information through the browser, which may cause conflicts or mismatched common character sets.
// This part is understood as request. setCharacterEncoding ("GBK"); set the body in the request, not the header part. When a get request is sent, the parameter is placed behind the url, not in the body. Therefore, the request is sent at this time. setCharacterEncoding ("GBK") does not play a role. If it is changed to post, there will be no problem. It has been tested and passed ,!!

The solution is:
1) Open the tomcat server. xml file, locate the block, and add the following line:
URIEncoding = "GBK"
The complete information should be as follows:
Copy codeThe Code is as follows:
<Connector uriencoding = "GBK" maxthreads = "150" debug = "0" redirectport = "8443" port = "8080" enablelookups = "false" maxsparethreads = "75" minsparethreads =" 25 "connectiontimeout =" 20000 "disableuploadtimeout =" true "acceptcount =" 100 "> </connector>

2) Restart tomcat. Everything is OK.
Tomact.org
This specifies the character encoding used to decode the URI bytes, after % xx decoding the URL. if not specified, ISO-8859-1 will be used. that is to say, the bytes and request of URI are converted. setCharacterEncoding ("GBK") converts the request. The problem is OK!

(4) The JSP page contains Chinese characters and buttons, but garbled characters appear when you view the page through the server:
Solution: first, the JSP file should not directly contain localized message text, but be obtained from the Resource Bundle using the <bean: message> tag. Put your Chinese text in Application. in the properties file, this file is placed in the WEB-INF/classes/*, for example, I have a name in the page, the age of two labels, I first want to create an Application. properties, the content should be name = "name" age = "age", then I put this file under the WEB-INF/classes/properties/, then according to the Application. properties file, encode and convert it, and create a Chinese resource file, assuming the name is Application_cn.properties. The native2ascii command is provided in JDK to convert character encoding. In the DOS environment, find the directory of the file where you placed Application. properties. In the DOS environment, execute the command to generate a GBK-Encoded chinese resource file Application_cn.properties: native2ascii? Encoding gbk Application. properties Application_cn.properties after executing the preceding command, the Application_cn.properties file: name = u59d3u540d age = u5e74u9f84 will be generated and configured in the Struts-config.xml: <message-resources parameter = "properties. application_cn "> </message-resources>. In this step, we have basically finished most of the work, and then you will write it on the JSP page. The label of the name is to write <bean: message key = "" name "">, in this way, the Chinese name will appear on the page. The same is true for age, and the processing of Chinese characters on the button is the same.
(5) garbled characters are written to the database:
Solution: configure a filter, that is, a Servelet filter. The Code is the same as in the second case.
If you directly connect to the database through JDBC, the configuration code is as follows: jdbc: mysql: // localhost: 3306/workshopdb? UseUnicode = true & characterEncoding = GBK to ensure that the code in the database is garbled.
If you are using a data source link, you cannot follow this method. First, you need to write it in the configuration file. In tomcat 5.0.19, the data source is configured in C: under Tomcat 5.0confCatalinalocalhost, the project I created is workshop, and the directory is under webapp, workshop. the xml configuration file is as follows:

Copy codeThe Code is as follows:
<Context debug = "0" path = "/workshop" docbase = "workshop"> </context> reloadable = "true">

<Resource name = "jdbc/WorkshopDB"> </resource> auth = "Container"
Type = "javax. SQL. DataSource"/>

<Resourceparams name = "jdbc/WorkshopDB"> </resourceparams>
<Parameter> </parameter>
<Name> </name> factory
<Value> </value> org. apache. commons. dbcp. basicperformancefactory

<Parameter> </parameter>
<Name> </name> maxActive
<Value> </value> 100

<Parameter> </parameter>
<Name> </name> maxIdle
<Value> </value> 30

<Parameter> </parameter>
<Name> </name> maxWait
<Value> </value> 10000

<Parameter> </parameter>
<Name> </name> username
<Value> </value> root

<Parameter> </parameter>
<Name> </name> password
<Value> </value>

<Parameter> </parameter>
<Name> </name> driverClassName
<Value> </value> com. mysql. jdbc. Driver

<Parameter> </parameter>
<Name> </name> url
<Value> </value>

Special attention should be paid to the bold text, which is different from the JDBC direct link. If you are configuring correctly, when you enter Chinese, it will become Chinese in the database, one thing to note is that you also need to use this line of code to display the data page. It should be noted that some front-end staff use Dreamver to write code after writing a Form, and change it to a jsp when writing a Form, so there is a place to pay attention, that is, in Dreamver, the Action submission method is request, and you need to submit it, because in the jsp submission process, there are two methods: POST and GET, however, the Code submitted by the two methods is quite different in coding, which will be explained later. </Bean: message>

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.