偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

常見(jiàn)JSP中文亂碼例子及其解決方法

開(kāi)發(fā) 后端
當(dāng)我們?cè)趯W(xué)習(xí)JSP開(kāi)發(fā)時(shí),中文亂碼是個(gè)比較常見(jiàn)的問(wèn)題,其根源是:Web容器默認(rèn)的字符處理編碼是ISO-8859-1。下面我們來(lái)詳細(xì)看看如何去解決這個(gè)問(wèn)題。

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

實(shí)例一、JSP頁(yè)面顯示時(shí)

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

運(yùn)行結(jié)果:

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

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

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

注冊(cè)頁(yè)面:

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <html> 
  3.     <head> 
  4.        <title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí)</title> 
  5.     </head> 
  6.     <body> 
  7.        <h2>申請(qǐng)賬號(hào):</h2> 
  8.        <form action="userMsg.jsp" method="POST"> 
  9.            <p>郵箱:&nbsp;<input type="text"name="email" id="email"/><p/> 
  10.            <p>昵稱(chēng):&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">一句話(huà)介紹自己...</textarea> 
  15.            <p><input type="submit"value="提交申請(qǐng)"></p> 
  16.        </form> 
  17.     </body> 
  18. </html> 

個(gè)人信息頁(yè)面:

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <html> 
  3.     <head> 
  4.        <title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí) </title> 
  5.     </head> 
  6.     <body> 
  7.        <center> 
  8.            <h2>用戶(hù)信息:</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>昵稱(chēng):&nbsp;<% out.print(nickname); %><p/> 
  16.            <p>密碼:&nbsp;<% out.print(password); %><p/> 
  17.            <p>性別:&nbsp;<% out.print(sex); %><p/> 
  18.            <p>個(gè)人介紹:<%out.print(introduction); %></p> 
  19.        </center> 
  20.     </body> 
  21. </html> 

運(yùn)行結(jié)果:

解決方法:修改個(gè)人信息頁(yè)面如下

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <html> 
  3.     <head> 
  4.        <title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí) </title> 
  5.     </head> 
  6.     <body> 
  7.        <h2>用戶(hù)信息:</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>昵稱(chēng): <% out.print(nickname); %><p/> 
  15.        <p>密碼: <% out.print(password); %><p/> 
  16.        <p>性別: <% out.print(sex); %><p/> 
  17.        <p>個(gè)人介紹:<%out.print(introduction); %></p> 
  18.     </body> 
  19. </html> 

實(shí)例三、Servlet處理中文參數(shù)時(shí)

注冊(cè)頁(yè)面:

  1. <%@ page contentType="text/html;charset=gb2312" %> 
  2. <%@ page import="test.UserMsg"%> 
  3. <html> 
  4.     <head> 
  5.        <title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí)</title> 
  6.     </head> 
  7.     <body> 
  8.        <h2>申請(qǐng)賬號(hào):</h2> 
  9.        <form action="./UserMsg" method="POST"> 
  10.            <p>郵箱: <input type="text"name="email" id="email"/><p/> 
  11.            <p>昵稱(chēng): <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">一句話(huà)介紹自己...</textarea> 
  16.            <p><input type="submit"value="提交申請(qǐng)"></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>" +"用戶(hù)信息:""</h2>");  
  31.            out.print("<p>"+"郵箱:"+request.getParameter("email")+"<p/>");  
  32.            out.print("<p>"+"昵稱(chēng):"+request.getParameter("nickname")+"<p/>");  
  33.            out.print("<p>"+"密碼:"+request.getParameter("password")+"<p/>");  
  34.            out.print("<p>"+"性別:"+request.getParameter("sex")+"<p/>");  
  35.            out.print("<p>"+"個(gè)人介紹:"+request.getParameter("introduction")+"<p/>");  
  36.            out.print("</html>");  
  37.            out.print("</body>");  
  38.       }  

運(yùn)行結(jié)果:

解決方法:在doPost中加入:

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

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

【編輯推薦】

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

2009-07-02 13:26:32

JSP中文亂碼

2009-07-01 18:14:36

JSP亂碼

2009-09-07 18:40:28

PHP中文亂碼

2009-07-02 13:32:56

JSP中文亂碼

2011-03-01 15:38:44

Fireftp亂碼

2009-02-18 14:28:23

編碼亂碼JSP

2013-06-20 09:14:43

2013-01-30 16:54:21

2010-07-15 14:01:12

telnet亂碼

2023-04-06 15:21:34

IIoT自動(dòng)化

2010-08-24 09:07:16

無(wú)線(xiàn)路由器

2011-06-14 13:41:27

muleWSDL

2010-10-13 17:22:12

MySQL查詢(xún)亂碼

2009-07-06 17:50:13

Java JSP

2021-03-25 11:25:43

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

2021-03-26 11:38:29

云計(jì)算

2024-11-08 13:47:35

中文亂碼配置

2024-05-24 12:06:26

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

2009-06-25 15:06:20

Javadoc亂碼

2009-06-30 13:49:21

excel文檔Jsp
點(diǎn)贊
收藏

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