JSP Chinese garbled display Solution

Source: Internet
Author: User
Tags html header glassfish

JSP Chinese garbled Processing Solution

 

Beginners JSP, especially Tomcat environment (glassfish default UTF-8, there is no such problem), often because of Chinese Character Set settings ProblemsChinese garbled charactersThis article provides a solution to this problem.Chinese garbled charactersFor your reference, I use a unified character setUTF-8And avoidGb2312TheChineseThe hidden danger of insufficient character sets. note three points. The default character set is the English character set.

1. Set the working mode of the server port in Tomcat server. xml file

2. unified processing of string character sets by Servlet and JavaBean

3. Unified JSP page Character Set

To unify these three points,ChineseThinkGarbledAre difficult, if you use MySQL, data tables also need to be unified UTF-8.

 

Modify the Tomcat server. xml file. The file is located in Tomcat/Conf, and the red part is the add part, that is, the current http port connection mode settings.

FindConnectorLabel to make the Tomcat serverUTF-8Mode to process garbled characters from the underlying layer. The modifications are as follows:

<Connector
Uriencoding = "UTF-8" Port = "8080" protocol = "HTTP/1.1"

Connectiontimeout = "20000"

Redirectport = "8443" type = "regxph" text = "yourobjectname"/>

Other application servers, such as glassfish, support UTF-8 by default. The server itself does not need to be set. You only need to pay attention to the code.

 

The settings in the running Code are as follows:

Before javajan and Servlet need to process Chinese characters, or add

Request. setcharacterencoding ("UTF-8 ");

Response. setcharacterencoding ("UTF-8 ");

Avoid Chinese characters garbled in JavaBean and servlet.

 

Java character encoding settings in JSP code. garbled code is processed on the page.

 

<% @ Page contenttype = "text/html" %>

<% @ Page pageencoding = "UTF-8" %>

 

Red indicates the JSP processing characterUTF-8Encoding method.

 

<% Request. setcharacterencoding ("UTF-8"); %>

Java code in JSP sets the receiving parameterUTF-8Encoding

 

Jsp html header encoding settings, page processing to UTF-8, to avoid display garbled.

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Title> note page </title>

</Head>

<Body>

......

......

Red partUTF-8Encoding method used for HTML pages

 

<Form action = index. jsp method = "Post">

<Input type = "text" size = "30" name = "Chinese name" value = "">

<Input type = submit value = "Submit">

</Form>

 

The receiving code is as follows:

 

<%

// Parameters passed through binary, becauseUTF-8Without conversion.Garbled.

String temp1 = request. getparameter ("yourname ");

......

%>

 

The get method is slightly different. If you only use the above settings, some text information will be lost due to different encoding in case of Chinese parameters. Therefore, you must encode the information before submitting the code. Here two functions are used.Java.net. urldecoder. DecodeAndJava.net. urldecoder. DecodeEncoding and decoding.

 

<A href ="

<% = Request. getcontextpath () %>/index. jsp? Passed parameter name =

<% = Java.net. urlencoder. encode (passed Chinese Character variable, "UTF-8") %> ">

<% = Hyperlink hotspot display content %>

</A>

 

Passed as a parameterChinese,
Java.net. urlencoder. encode
The variable to be sent by the function is parsed to a hexadecimal number for URL transfer. The receiving code is as follows:

 

<% String STR = request. getparameter ("passed parameter name"); %>

 

Therefore,StrIs the correct Chinese information, completely avoidingGarbled.

 

Available java.net. urldecoder. Decode Functions

For example

<%

String str1 = java.net. urldecoder. Decode (request. getparameter ("passed parameter name"), "UTF-8 ");

%>

 

The garbled Chinese character parameters will be perfectly solved.

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.