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
- <Html>
- <Head>
- <Title> Chinese Garbled text-when the JSP page is displayed </title>
- </Head>
- <Body>
- <Center>
- <Br/>
- <H1> Mulan's words are like ancient words and friends
- <P> if life is just like the first sight, what is autumn wind and sorrow. </P>
- <P> when you are idle, you are easy to change. </P>
- <P> when the slogan and slogan. </P>
- <P> He ruo, Jin Yilang, is willing to join the current day. </P>
- </Center>
- </Body>
- </Html>
Running result:
Solution: Specify the Chinese character set for it, and add the following before
- <%@ page contentType="text/html;charset=gb2312" %>
Example 2: When the JSP page passes Chinese Parameters
Registration page:
- <% @ Page contentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
- </Head>
- <Body>
- <H2> apply for an account:
- <Form action = "userMsg. jsp" method = "POST">
- <P> email: & nbsp; <input type = "text" name = "email" id = "email"/> <p/>
- <P> nickname: & nbsp; <input type = "text" name = "nickname" id = "nickname"/> <p/>
- & 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:
- <% @ Page contentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
- </Head>
- <Body>
- <Center>
- <H2> User information:
- <% String email = request. getParameter ("email"); %>
- <% String nickname = request. getParameter ("nickname"); %>
- <% String password = request. getParameter ("password"); %>
- <% String sex = request. getParameter ("sex"); %>
- <% String introduction = request. getParameter ("introduction"); %>
- <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:
- <% @ Page contentType = "text/html; charset = gb2312" %>
- <Html>
- <Head>
- <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
- </Head>
- <Body>
- <H2> User information:
- <% String email = newString (request. getParameter ("email"). getBytes ("ISO-8859-1"), "gb2312"); %>
- <% String nickname = newString (request. getParameter ("nickname"). getBytes ("ISO-8859-1"), "gb2312"); %>
- <% String password = newString (request. getParameter ("password"). getBytes ("ISO-8859-1"), "gb2312"); %>
- <% String sex = newString (request. getParameter ("sex"). getBytes ("ISO-8859-1"), "gb2312"); %>
- <% String introduction = newString (request. getParameter ("introduction"). getBytes ("ISO-8859-1"), "gb2312"); %>
- <P> email: <% out. print (email); %> <p/>
- &
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:
- <% @ Page contentType = "text/html; charset = gb2312" %>
- <% @ Page import = "test. UserMsg" %>
- <Html>
- <Head>
- <Title> Chinese Garbled text-when the JSP page passes Chinese parameters </title>
- </Head>
- <Body>
- <H2> apply for an account:
- <Form action = "./UserMsg" method = "POST">
- <P> email: <input type = "text" name = "email" id = "email"/> <p/>
- <P> nickname: <input type = "text" name = "nickname" id = "nickname"/> <p/>
- <P> password: <input type = "password" name = "password" id = "password"/> <p/>
- <P> Gender: <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>
- & Nb
Sp; </form>
UserMsg. java (Servlet)
- package test;
-
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.io.UnsupportedEncodingException;
-
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- public classUserMsg extends HttpServlet{
- public void doGet(HttpServletRequestrequest,
- HttpServletResponse response){
- doPost(request, response);
- }
- public void doPost(HttpServletRequestrequest,
- HttpServletResponse response){
- try {
- request.setCharacterEncoding("gb2312");
- } catch (UnsupportedEncodingExceptione) {
- e.printStackTrace();
- }
- PrintWriter out = null;
- try {
- out = response.getWriter();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- out.print("
- &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:
- response.setContentType("text/html; charset=gb2312");