How to solve garbled characters in JSP get receiving data

Source: Internet
Author: User

Response. sendRedirect ("url? Parameter "); at this time, if the parameter we want to pass is Chinese, it is very likely that it will be displayed in garbled characters after it is passed. This is because response. sendRedirec ("url? Parameter. So there will be garbled issues. So how can we solve it? First, import the java.net. *; Package on the value passing page, and then use the URLEncoder. encode (String str) method in the package to convert the Chinese parameter to another encoding format. In this way, the code of the % DB % FD % style will not be displayed as garbled characters when passed in the url. The second step is to transcode the parameter to the Chinese format on the read parameter page. The method is new String (the read parameter. getBytes ("ISo-8859-1"), "UTF-8"); then receive the passed Chinese value with a String type variable. For example, suppose there are pages a. jsp and B. jsp. And the pageEncoding of both pages is GBK. jsp to B. pass a "Hello" in jsp;. jsp <% @ import java.net. * %> <% Stirng str = "hello"; str = str. urlEncoder. encode (str); response. sendRedirect ("B. jsp? Name = "+ str); %> B. jsp <% String getName = new String (request. getParameter ("name "). getBytes ("iso-8859-1"), "GBK"); out. println (getName); %> in this way, B. jsp can output "hello" normally. Note: In B. jsp, you must add a new string to receive the read information. Otherwise, garbled characters may occur.

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.