Common examples of Chinese garbled JSP code and Solutions

Source: Internet
Author: User

JSP development application is, Chinese garbled characters is a more common problem, its root cause is: Web Container default Character Processing encoding is ISO-8859-1.

Instance 1: When the JSP page is displayed

 
  1. <Html>
  2. <Head>
  3. <Title> Chinese Garbled text-when the JSP page is displayed </title>
  4. </Head>
  5. <Body>
  6. <Center>
  7. <Br/>
  8. <H1> Mulan's words are like ancient words and friends
  9. <P> if life is just like the first sight, what is autumn wind and sorrow. </P>
  10. <P> when you are idle, you are easy to change. </P>
  11. <P> when the slogan and slogan. </P>
  12. <P> He ruo, Jin Yilang, is willing to join the current day. </P>
  13. </Center>
  14. </Body>
  15. </Html>

Running result:

Solution: Specify the Chinese character set for it, and add the following before

 
  1. <%@ page contentType="text/html;charset=gb2312" %> 

Example 2: When the JSP page passes Chinese Parameters

Registration page:

 
  1. <% @ Page contentType = "text/html; charset = gb2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
  5. </Head>
  6. <Body>
  7. <H2> apply for an account:
  8. <Form action = "userMsg. jsp" method = "POST">
  9. <P> email: & nbsp; <input type = "text" name = "email" id = "email"/> <p/>
  10. <P> nickname: & nbsp; <input type = "text" name = "nickname" id = "nickname"/> <p/>
  11. & N

Bsp; <p> password: & nbsp; <input type = "password" name = "password" id = "password"/> <p/>

  • <P> Gender: & nbsp; <input type = "radio" name = "sex" id = "sex" value = "male"/> male
  • <Input type = "radio" name = "sex" id = "sex" value = "female"/> female <p/>
  • <Textarea name = "introduction" id = "introduction" rows = "5" cols = "27"> introduce yourself in one sentence... </textarea>
  • <P> <input type = "submit" value = "submit Application"> </p>
  • </Form>
  • </Body>
  • </Html>

Personal information page:

 
  1. <% @ Page contentType = "text/html; charset = gb2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
  5. </Head>
  6. <Body>
  7. <Center>
  8. <H2> User information:
  9. <% String email = request. getParameter ("email"); %>
  10. <% String nickname = request. getParameter ("nickname"); %>
  11. <% String password = request. getParameter ("password"); %>
  12. <% String sex = request. getParameter ("sex"); %>
  13. <% String introduction = request. getParameter ("introduction"); %>
  14. <P> Email: & nbsp; <

; % Out. print (email); %> <p/>

  • <P> nickname: & nbsp; <% out. print (nickname) ;%> <p/>
  • <P> password: & nbsp; <% out. print (password) ;%> <p/>
  • <P> Gender: & nbsp; <% out. print (sex) ;%> <p/>
  • <P> personal introduction: <% out. print (introduction); %> </p>
  • </Center>
  • </Body>
  • </Html>

Running result:

Solution: the Personal Information Modification page is as follows:

 
  1. <% @ Page contentType = "text/html; charset = gb2312" %>
  2. <Html>
  3. <Head>
  4. <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
  5. </Head>
  6. <Body>
  7. <H2> User information:
  8. <% String email = newString (request. getParameter ("email"). getBytes ("ISO-8859-1"), "gb2312"); %>
  9. <% String nickname = newString (request. getParameter ("nickname"). getBytes ("ISO-8859-1"), "gb2312"); %>
  10. <% String password = newString (request. getParameter ("password"). getBytes ("ISO-8859-1"), "gb2312"); %>
  11. <% String sex = newString (request. getParameter ("sex"). getBytes ("ISO-8859-1"), "gb2312"); %>
  12. <% String introduction = newString (request. getParameter ("introduction"). getBytes ("ISO-8859-1"), "gb2312"); %>
  13. <P> email: <% out. print (email); %> <p/>
  14. &


Lt; p> nickname: <% out. print (nickname); %> <p/>

  • <P> password: <% out. print (password) ;%> <p/>
  • <P> Gender: <% out. print (sex); %> <p/>
  • <P> personal introduction: <% out. print (introduction); %> </p>
  • </Body>
  • </Html>

Example 3: When the Servlet processes Chinese Parameters

Registration page:

 
  1. <% @ Page contentType = "text/html; charset = gb2312" %>
  2. <% @ Page import = "test. UserMsg" %>
  3. <Html>
  4. <Head>
  5. <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
  6. </Head>
  7. <Body>
  8. <H2> apply for an account:
  9. <Form action = "./UserMsg" method = "POST">
  10. <P> email: <input type = "text" name = "email" id = "email"/> <p/>
  11. <P> nickname: <input type = "text" name = "nickname" id = "nickname"/> <p/>
  12. <P> password: <input type = "password" name = "password" id = "password"/> <p/>
  13. <P> Gender: <input type = "radio" name = "sex" id = "sex" value = "male"/> male
  14. <Input type = "radio" name = "sex" id = "sex" value = "female"/> female <p/>
  15. <Textarea name = "introduction" id = "introduction" rows = "5" cols = "27"> introduce yourself in one sentence... </textarea>
  16. <P> <input type = "submit" value = "submit Application"> </p>
  17. & Nb


Sp; </form>

  • </Body>
  • </Html>

UserMsg. java (Servlet)

 
  1. package test;  
  2.    
  3. importjava.io.IOException;  
  4. importjava.io.PrintWriter;  
  5. importjava.io.UnsupportedEncodingException;  
  6.    
  7. importjavax.servlet.http.HttpServlet;  
  8. importjavax.servlet.http.HttpServletRequest;  
  9. importjavax.servlet.http.HttpServletResponse;  
  10. public classUserMsg extends HttpServlet{  
  11.       public void doGet(HttpServletRequestrequest,  
  12.                  HttpServletResponse response){  
  13.            doPost(request, response);  
  14.       }  
  15.       public void doPost(HttpServletRequestrequest,  
  16.                  HttpServletResponse response){  
  17.            try {  
  18.                  request.setCharacterEncoding("gb2312");  
  19.            } catch (UnsupportedEncodingExceptione) {  
  20.                  e.printStackTrace();  
  21.            }  
  22.            PrintWriter out = null;  
  23.            try {  
  24.                  out = response.getWriter();  
  25.            } catch (IOException e1) {  
  26.                  e1.printStackTrace();  
  27.            }  
  28.            out.print("
  29.    &nbs


P; out. print ("<body> ");

  • Out. print ("
  • Out. print ("<p>" + "email:" + request. getParameter ("email") + "<p/> ");
  • Out. print ("<p>" + "nickname:" + request. getParameter ("nickname") + "<p/> ");
  • Out. print ("<p>" + "password:" + request. getParameter ("password") + "<p/> ");
  • Out. print ("<p>" + "Gender:" + request. getParameter ("sex") + "<p/> ");
  • Out. print ("<p>" + "Personal Profile:" + request. getParameter ("introduction") + "<p/> ");
  • Out. print ("
  • Out. print ("</body> ");
  • }
  • }

Running result:

Solution: add the following to doPost:

 
  1. response.setContentType("text/html; charset=gb2312"); 

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.