Solution to jsp Chinese page garbled text and parameter passing garbled text

Source: Internet
Author: User
Tags character set

Page garbled

The reason for this garbled code 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, www.111cn.net

The code is as follows: Copy code

<% @ 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: Copy code
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: Copy code
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: Copy code
String name = request. getParameter ("name ");

Change

The code is as follows: Copy code
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: Copy code
Function toView (param ){
Var encodeparam = encodeURI (param );
Window. location. href = "/naias/advAna_form.do? Method = showDetail & title = "+ encodeparam;
    }

2. Convert the character set in the jump Action: www.111cn.net

The code is as follows: Copy code
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: Copy code
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: Copy code
Request. setCharacterEncoding ("UTF-8 ");

To declare the encoding type.

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.