自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
當(dāng)我們在學(xué)習(xí)JSP開發(fā)時,中文亂碼是個比較常見的問題,其根源是:Web容器默認的字符處理編碼是ISO-8859-1。下面我們來詳細看看如何去解決這個問題。

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

實例一、JSP頁面顯示時

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

運行結(jié)果:

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

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

實例二、JSP頁面?zhèn)鬟f中文參數(shù)時

注冊頁面:

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <html> 
  3.     <head> 
  4.        <title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時</title> 
  5.     </head> 
  6.     <body> 
  7.        <h2>申請賬號:</h2> 
  8.        <form action="userMsg.jsp" method="POST"> 
  9.            <p>郵箱:&nbsp;<input type="text"name="email" id="email"/><p/> 
  10.            <p>昵稱:&nbsp;<input type="text"name="nickname" id="nickname"/><p/> 
  11.            <p>密碼:&nbsp;<input type="password"name="password" id="password"/><p/> 
  12.            <p>性別:&nbsp;<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> 

個人信息頁面:

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <html> 
  3.     <head> 
  4.        <title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時 </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>郵箱:&nbsp;<% out.print(email); %><p/> 
  15.            <p>昵稱:&nbsp;<% out.print(nickname); %><p/> 
  16.            <p>密碼:&nbsp;<% out.print(password); %><p/> 
  17.            <p>性別:&nbsp;<% out.print(sex); %><p/> 
  18.            <p>個人介紹:<%out.print(introduction); %></p> 
  19.        </center> 
  20.     </body> 
  21. </html> 

運行結(jié)果:

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

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <html> 
  3.     <head> 
  4.        <title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時 </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處理中文參數(shù)時

注冊頁面:

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <%@ page import="test.UserMsg"%> 
  3. <html> 
  4.     <head> 
  5.        <title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時</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)

  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.       }  

運行結(jié)果:

解決方法:在doPost中加入:

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

原文鏈接:http://blog.csdn.net/cannel_2020/article/details/7375768

【編輯推薦】

  1. 告別無止境的增刪改查:Java代碼生成器
  2. Java通過SSH2協(xié)議運行遠程程序
  3. Java Bean屬性命名規(guī)范問題分析
  4. Java Socket編程:初識TCP Socket
  5. Java Socket編程:如何識別網(wǎng)絡(luò)主機
責(zé)任編輯:林師授 來源: Cannel_2020的博客
相關(guān)推薦

2009-07-02 13:26:32

JSP中文亂碼

2009-09-07 18:40:28

PHP中文亂碼

2009-07-02 13:32:56

JSP中文亂碼

2009-07-01 18:14:36

JSP亂碼

2011-03-01 15:38:44

Fireftp亂碼

2009-02-18 14:28:23

編碼亂碼JSP

2013-01-30 16:54:21

2013-06-20 09:14:43

2010-07-15 14:01:12

telnet亂碼

2023-04-06 15:21:34

IIoT自動化

2011-06-14 13:41:27

muleWSDL

2010-10-13 17:22:12

MySQL查詢亂碼

2010-08-24 09:07:16

無線路由器

2009-07-06 17:50:13

Java JSP

2024-11-08 13:47:35

中文亂碼配置

2024-05-24 12:06:26

SQL數(shù)據(jù)庫

2021-03-26 11:38:29

云計算

2021-03-25 11:25:43

云計算云計算產(chǎn)業(yè)云應(yīng)用

2009-06-25 15:06:20

Javadoc亂碼

2010-05-14 17:27:40

MySQL中文亂碼
點贊
收藏

51CTO技術(shù)棧公眾號