Question about passing Chinese Parameters Using URL encoding in JSP ※※chinese parameters garbled in JSP

Source: Internet
Author: User

Question about passing Chinese Parameters Using URL encoding in JSP

When the JSP page uses URL encoding to pass Chinese parameters, garbled characters may occur during Parameter Parsing. Due to Java design considerations to the international problem, the Java source code compiled into bytecode when the default use of UTF-8 encoding. In terms of Web application, because different browsers use different encoding methods to send messages to the server, garbled characters are generated due to different encoding methods when decoded by servers such as Tomcat. This is a problem that will plague JSP beginners for a long time. We didn't need to deal with these problems when using struts in the past. We encountered this problem when we made a simple JSP page a few days ago. After half a day of exploration, the problem was basically solved. In this example, the. jsp page passes the Chinese parameter through URL encoding and parses the parameter in B. jsp. A. JSP Source Code <% @ page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. * "errorpage =" "%> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> The solution to the problem of passing Chinese parameters garbled characters in JSP is found to be garbled when the parameters are passed to the processing page as parameters. I have heard that this problem is caused by encoding problems, but it cannot be solved. Today, we finally found a solution. Simple: Add the following three lines to each JSP page <% @ page pageencoding = "GBK" %> <% @ page contenttype = "text/html; charset = GBK "%> <% request. setcharacterencoding ("GBK"); %> You can change GBK to gb2312. The difference is that GBK not only supports Simplified Chinese, but also traditional Chinese, while gb2312 only supports Simplified Chinese.

Summary on the problem of passing Chinese parameters, spaces, and form content line feed in JSP

I. Question about Chinese/space of address passing Parameters
Example:
String scontent = "Chinese Parameters ";
If you do not do any processing, it will be passed directly. The Chinese character may be correct (but not guaranteed), but the space will certainly be faulty and cannot be recognized on the receiving page.
Solution 1: java.net. urlencoder. encode (scontent, "gb2312"); then pass. For example, the URL is "Accept. jsp? Content = "+ scontent;
When reading, use
String SC = request. getparameter ("content ");
SC = new string (SC. getbytes ("iso-8859-1"), "gb2312 ");

Solution 2:

Replace the following before passing

Scontent = scontent. replaceall ("", "% 20 ");

After receiving the string, if it is displayed in TD, use

Scontent = scontent. replaceall ("", "& nbsp;"); // note that % 20 is not replaced, but spaces are replaced. If you have Chinese characters, you must first transcode them.

If it is displayed in textarea, no processing is required.

So there is no problem.
Ii. newline
Problem description:
The textarea in the form contains line breaks. When submitted, it is saved to the database, and then read, there is no line breaks, and all the lines are connected together.
Solution:
1. When writing data to the database, add
Scontent = scontent. replaceall ("", "& nbsp ;");
Scontent = scontent. replaceall ("/R/N", "<br/> ");
Scontent = scontent. replaceall ("/N", "<br/> ");
2. If you want to display the data in textarea during reading, you must add
SC = value of the content field in the database;
SC = SC. replaceall ("<br/>", "/R/N ");
SC = SC. replaceall ("<br>", "/R/N ");
SC = SC. replaceall ("& nbsp ;","");
If it is directly displayed in TD, no conversion is required.

In fact, this is the post below. I recently found out what the problem is, and I will re-write the specific points.
But I don't know how to solve it. I don't know if any experts have come up to solve it?
Below is my code (it is a page display problem)
D1 and D2 are the selection condition parameters obtained from the previous page. They are used in SQL queries.
<%
String d1 = request. getparameter ("d1 ");
If (d1 = NULL)
{D1 = "";
}
Byte B1 [] = d1.getbytes ("ISO-8859-1 ");
D1 = new string (B1 );

String D2 = request. getparameter ("D2 ");
If (d2 = NULL)
{D2 = "";
}
Byte B2 [] = d2.getbytes ("ISO-8859-1 ");
D2 = new string (B2 );
Out. Print (D1 );
Out. Print (D2); // to check whether D1 and D2 are passed in, let him display it!
%>
This is a super connection, and page is the current page Code required for pagination.
<A href = "gagea. jsp? Page = <% = intpage + 1%> & d1 = <% = D1 %> & D2 = <% = d2 %> "> next page </a>

When I click the link, the webpage address bar is displayed correctly (the three parameters are passed), but the pages are all ??????, I want to solve the problem of passing Chinese parameters, but I don't know how to solve it.
Although the Code is not detailed, I believe the experts will know what the problem is at first glance. Please help me. Thank you. In the past two days, my head has been enlarged due to this problem, depressed!

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.