Garbled jsp Chinese pages and passing Parameters

Source: Internet
Author: User

Page garbled

The reason for this Garbled text is that the character set encoding is not specified on the page. Solution: Use the following code to specify the character set encoding at the beginning of the page.

The Code is as follows:  

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "err. jsp" %>

Database garbled

This Garbled text will make Chinese characters you inserted into the database become Garbled text, or it will also be garbled when you read the display,

The solution is as follows:

Add the encoding character set to the database connection string

The Code is as follows:  
String Url = "jdbc: mysql: // localhost/digitgulf? User = root & password = root & useUnicode = true & characterEncoding = GB2312 ";

Use the following code on the page:

The Code is as follows:  
Response. setContentType ("text/html; charset = gb2312 ");
Request. setCharacterEncoding ("gb2312 ");

Garbled URLs

Method 1:

1. Change pageEncoding = "GB2312" to pageEncoding = "ISO8859-1" in B. jsp"

Although the content displayed on the B. jsp page is garbled, it is not the type of "???" But some special characters.

2. Check the menu in the browser and modify the code to GB2312. Then, the Garbled text will display Chinese characters.

3. However, this method is not feasible.

Method 2:

1. In B. jsp

The Code is as follows:  
String name = request. getParameter ("name ");

Change

The Code is as follows:  
String name = new String (request. getParameter ("name"). getBytes ("ISO-8859-1"), "GB2312 ");

2. Then it will be displayed on the page, which is Chinese.

Method 3:

The Request Parameters on the request page need to be transcoded using encodeURI, and Character Set conversion is required on the page receiving the request. The character set for the request and receive pages in this example are both UTF-8:

1. The requested JSP page uses the JavaScript encodeURI () method to transcode the parameters:

The Code is as follows:  
Function toView (param ){
Var encodeparam = encodeURI (param );
Window. location. href = "/naias/advAna_form.do? Method = showDetail & title = "+ encodeparam;
}

2. Perform Character Set conversion in the jump Action: www. bKjia. c0m

The Code is as follows:  
Public ActionForward excute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ){
String title = request. getParameter ("title ");
Title = new String (title. getBytes ("iso-8859-1"), "UTF-8 ");
System. out. println (title );
}

For the JSP page parameters encoded as UTF-8, for the Chinese garbled solution is divided into two situations:

1. Pass the parameter through the GET method

The Code is as follows:  
New String (request. getParameter (""). getBytes ("ISO-8859-1"), "UTF-8 ");

To the required encoding, such as UTF-8.

2. Pass parameters through POST and declare

The Code is as follows:  
Request. setCharacterEncoding ("UTF-8 ");

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.