Java Development must read to solve the Java EE garbled problem (1)

Source: Internet
Author: User

BKJIA once introduced many garbled issues, such as "in-depth analysis of Python Chinese garbled instructions" and "Introduction to Ado. Net garbled code. This article makes a summary of the garbled characters in Java EE in order to give you some reference. InJava EEGarbled text is a headache. To solve these problems quickly and accurately, we must start with the crux of the problem.

First, we know that the reason for Garbled text is due to different codes. This is like talking to an Indian. Suppose you do not understand the Indian language at all, and the other party does not understand the Chinese language, then what you said will generate question marks in each other's mind. So what should we do? The common practice is to find a translation.JavaFor encoding, let's look at an example.

Method 1:

 
 
  1. 1
  2. 2 <body>
  3. 3 <form action = "demo02.jsp" method = "post">
  4. 4 User name: <input type = "text" name = "uname">
  5. 5 <input type = "submit" value = "go">
  6. 6 </form>
  7. 7 </body>
  8. 8
  9. <% @ Page contentType = "text/html; charset = gbk" %>
  10. <Html>
  11. <Body>
  12. </Body>
  13. </Html>

Processing deom01.htmlJSPTo retrieve and display the Uname value of Demo01,

Input content :??

When we enter Chinese Characters in Deom01 and submit them, the displayed content after submission is "?" Number. Here, it is equivalent to the speech of people in different languages above, resulting in "?" The solution is to solve this problem through conversion:

 
 
  1. <% @ Page contentType = "text/html; charset = gbk" %>
  2. <Html>
  3. <Body>
  4. <%
  5. /** // * String name = request. getParmaeter ("uname"); // obtain the uname Value
  6. Byte [] B = name. getBytes ("ISO8859-1"); // convert the obtained value to a Byte array
  7. Name = new String (B); // convert the Byte array to the required format.
  8. */
  9. String name = new String (request. getParameter ("uname"). getBytes ("ISO8859-1"); // It is equivalent to the above
  10. %>
  11. <H1> input content: <% = uname %>
  12. <% -- <H1> input content: <% = request. getParameter ("uname") %>
  13. </Body>
  14. </Html>
  15.  

The above code solves the garbled problem by converting the obtained uname value to a Byte array and then converting the Byte array to the required format.


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.