常見JSP中文亂碼例子及其解決方法

JSP開發應用是,中文亂碼是個比較常見的問題,其根源是:Web容器默認的字符處理編碼是ISO-8859-1。

實例一、JSP頁面顯示時

[html] view plaincopy
  1. <html>  
  2.     <head>  
  3.        <title>中文亂碼——JSP頁面顯示時</title>  
  4.     </head>  
  5.     <body>  
  6.        <center>  
  7.            <br/>  
  8.            <h1>木蘭辭擬古決絕詞柬友</h1>  
  9.            <p>人生若只如初見,何事秋風悲畫扇。</p>  
  10.          <p>等閒變卻故人心,卻道故人心易變。</p>  
  11.          <p>驪山語罷清宵半,淚雨霖鈴終不怨。</p>  
  12.          <p>何如薄倖錦衣郎,比翼連枝當日願。</p>  
  13.        </center>  
  14.     </body>  
  15. </html>  

運行結果:

 

解決方法:爲其指定中文字符集,<html>前加入

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


實例二、JSP頁面傳遞中文參數時

註冊頁面:

[html] view plaincopy
  1. <%@ page contentType="text/html;charset=gb2312" %>  
  2. <html>  
  3.     <head>  
  4.        <title>中文亂碼——JSP頁面傳遞中文參數時</title>  
  5.     </head>  
  6.     <body>  
  7.        <h2>申請賬號:</h2>  
  8.        <form action="userMsg.jsp" method="POST">  
  9.            <p>郵箱: <input type="text"name="email" id="email"/><p/>  
  10.            <p>暱稱: <input type="text"name="nickname" id="nickname"/><p/>  
  11.            <p>密碼: <input type="password"name="password" id="password"/><p/>  
  12.            <p>性別: <input type="radio"name="sex" id="sex"value="男" /> 男  
  13.                          <input type="radio" name="sex"id="sex" value="女" /> 女<p/>  
  14.            <textarea  name="introduction"id="introduction" rows="5" cols="27">一句話介紹自己...</textarea>  
  15.            <p><input type="submit"value="提交申請"></p>  
  16.        </form>  
  17.     </body>  
  18. </html>  

個人信息頁面:

[html] view plaincopy
  1. <%@ page contentType="text/html;charset=gb2312" %>  
  2. <html>  
  3.     <head>  
  4.        <title>中文亂碼——JSP頁面傳遞中文參數時 </title>  
  5.     </head>  
  6.     <body>  
  7.        <center>  
  8.            <h2>用戶信息:</h2>  
  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>郵箱: <% out.print(email); %><p/>  
  15.            <p>暱稱: <% out.print(nickname); %><p/>  
  16.            <p>密碼: <% out.print(password); %><p/>  
  17.            <p>性別: <% out.print(sex); %><p/>  
  18.            <p>個人介紹:<%out.print(introduction); %></p>  
  19.        </center>  
  20.     </body>  
  21. </html>  

運行結果:

解決方法:修改個人信息頁面如下

[html] view plaincopy
  1. <%@ page contentType="text/html;charset=gb2312" %>  
  2. <html>  
  3.     <head>  
  4.        <title>中文亂碼——JSP頁面傳遞中文參數時 </title>  
  5.     </head>  
  6.     <body>  
  7.        <h2>用戶信息:</h2>  
  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>郵箱: <% out.print(email); %><p/>  
  14.        <p>暱稱: <% out.print(nickname); %><p/>  
  15.        <p>密碼: <% out.print(password); %><p/>  
  16.        <p>性別: <% out.print(sex); %><p/>  
  17.        <p>個人介紹:<%out.print(introduction); %></p>  
  18.     </body>  
  19. </html>  


實例三、Servlet處理中文參數時

註冊頁面:

[html] view plaincopy
  1. <%@ page contentType="text/html;charset=gb2312" %>  
  2. <%@ page import="test.UserMsg"%>  
  3. <html>  
  4.     <head>  
  5.        <title>中文亂碼——JSP頁面傳遞中文參數時</title>  
  6.     </head>  
  7.     <body>  
  8.        <h2>申請賬號:</h2>  
  9.        <form action="./UserMsg" method="POST">  
  10.            <p>郵箱: <input type="text"name="email" id="email"/><p/>  
  11.            <p>暱稱: <input type="text"name="nickname" id="nickname"/><p/>  
  12.            <p>密碼: <input type="password"name="password" id="password"/><p/>  
  13.            <p>性別: <input type="radio"name="sex" id="sex"value="男" /> 男  
  14.                          <input type="radio" name="sex"id="sex" value="女" /> 女<p/>  
  15.            <textarea  name="introduction"id="introduction" rows="5" cols="27">一句話介紹自己...</textarea>  
  16.            <p><input type="submit"value="提交申請"></p>  
  17.        </form>  
  18.     </body>  
  19. </html>  

UserMsg.java(Servlet)

[java] view plaincopy
  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("<html>");  
  29.            out.print("<body>");  
  30.            out.print("<h2>" +"用戶信息:""</h2>");  
  31.            out.print("<p>"+"郵箱:"+request.getParameter("email")+"<p/>");  
  32.            out.print("<p>"+"暱稱:"+request.getParameter("nickname")+"<p/>");  
  33.            out.print("<p>"+"密碼:"+request.getParameter("password")+"<p/>");  
  34.            out.print("<p>"+"性別:"+request.getParameter("sex")+"<p/>");  
  35.            out.print("<p>"+"個人介紹:"+request.getParameter("introduction")+"<p/>");  
  36.            out.print("</html>");  
  37.            out.print("</body>");  
  38.       }  
  39. }  
運行結果:


解決方法:在doPost中加入:

[java] view plaincopy
  1. response.setContentType("text/html; charset=gb2312");  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章