濮阳杆衣贸易有限公司

主頁 > 知識庫 > JSP中文亂碼常見3個例子及其解決方法

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

熱門標簽:電銷機器人是有一些什么技術 北票市地圖標注 四川保險智能外呼系統(tǒng)商家 電銷機器人好賣么 杭州ai語音電銷機器人功能 高德地圖標注樣式 地圖標注線上教程 商洛電銷 杭州語音電銷機器人軟件

常見3個例子及其解決方法如下

實例一、JSP頁面顯示時

html> 
  head> 
    title>中文亂碼——JSP頁面顯示時/title> 
  /head> 
  body> 
    center> 
      br/> 
      h1>木蘭辭擬古決絕詞柬友/h1> 
      p>人生若只如初見,何事秋風悲畫扇。/p> 
    p>等閑變卻故人心,卻道故人心易變。/p> 
    p>驪山語罷清宵半,淚雨霖鈴終不怨。/p> 
    p>何如薄幸錦衣郎,比翼連枝當日愿。/p> 
    /center> 
  /body> 
/html>

運行結果:

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

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

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

注冊頁面:

%@ page contentType="text/html;charset=gb2312" %> 
html> 
  head> 
    title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時/title> 
  /head> 
  body> 
    h2>申請賬號:/h2> 
    form action="userMsg.jsp" method="POST"> 
      p>郵箱:nbsp;input type="text"name="email" id="email"/>p/> 
      p>昵稱:nbsp;input type="text"name="nickname" id="nickname"/>p/> 
      p>密碼:nbsp;input type="password"name="password" id="password"/>p/> 
      p>性別:nbsp;input type="radio"name="sex" id="sex"value="男" /> 男 
             input type="radio" name="sex"id="sex" value="女" /> 女p/> 
      textarea name="introduction"id="introduction" rows="5" cols="27">一句話介紹自己.../textarea> 
      p>input type="submit"value="提交申請">/p> 
    /form> 
  /body> 
/html> 

個人信息頁面:

%@ page contentType="text/html;charset=gb2312" %> 
html> 
  head> 
    title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時 /title> 
  /head> 
  body> 
    center> 
      h2>用戶信息:/h2> 
      % 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>郵箱:nbsp;% out.print(email); %>p/> 
      p>昵稱:nbsp;% out.print(nickname); %>p/> 
      p>密碼:nbsp;% out.print(password); %>p/> 
      p>性別:nbsp;% out.print(sex); %>p/> 
      p>個人介紹:%out.print(introduction); %>/p> 
    /center> 
  /body> 
/html> 

運行結果:

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

%@ page contentType="text/html;charset=gb2312" %> 
html> 
  head> 
    title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時 /title> 
  /head> 
  body> 
    h2>用戶信息:/h2> 
    % 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>郵箱: % out.print(email); %>p/> 
    p>昵稱: % out.print(nickname); %>p/> 
    p>密碼: % out.print(password); %>p/> 
    p>性別: % out.print(sex); %>p/> 
    p>個人介紹:%out.print(introduction); %>/p> 
  /body> 
/html> 

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

注冊頁面:

%@ page contentType="text/html;charset=gb2312" %> 
%@ page import="test.UserMsg"%> 
html> 
  head> 
    title>中文亂碼——JSP頁面?zhèn)鬟f中文參數(shù)時/title> 
  /head> 
  body> 
    h2>申請賬號:/h2> 
    form action="./UserMsg" method="POST"> 
      p>郵箱: input type="text"name="email" id="email"/>p/> 
      p>昵稱: input type="text"name="nickname" id="nickname"/>p/> 
      p>密碼: input type="password"name="password" id="password"/>p/> 
      p>性別: input type="radio"name="sex" id="sex"value="男" /> 男 
             input type="radio" name="sex"id="sex" value="女" /> 女p/> 
      textarea name="introduction"id="introduction" rows="5" cols="27">一句話介紹自己.../textarea> 
      p>input type="submit"value="提交申請">/p> 
    /form> 
  /body> 
/html> 

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("html>"); 
      out.print("body>"); 
      out.print("h2>" +"用戶信息:"+ "/h2>"); 
      out.print("p>"+"郵箱:"+request.getParameter("email")+"p/>"); 
      out.print("p>"+"昵稱:"+request.getParameter("nickname")+"p/>"); 
      out.print("p>"+"密碼:"+request.getParameter("password")+"p/>"); 
      out.print("p>"+"性別:"+request.getParameter("sex")+"p/>"); 
      out.print("p>"+"個人介紹:"+request.getParameter("introduction")+"p/>"); 
      out.print("/html>"); 
      out.print("/body>"); 
   } 
} 

運行結果:

解決方法:在doPost中加入:

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

以上就是幾種常見JSP中文亂碼例子及其解決方法,希望能夠幫助大家解決JSP中文亂碼的問題。

您可能感興趣的文章:
  • jsp用過濾器解決中文亂碼問題的方法
  • JSP對URL鏈接中的中文亂碼處理方法總結
  • JSP利用過濾器解決request中文亂碼問題
  • JSP頁面中超鏈接傳遞中文參數(shù)出現(xiàn)亂碼問題解決方法
  • 分享JSP中文亂碼解決方法
  • JSP頁面?zhèn)鲄⒊霈F(xiàn)中文亂碼的解決方案
  • jsp之間傳參數(shù)接受中文有亂碼問題解決方法
  • jsp地址欄傳中文顯示亂碼解決方法分享
  • JSP出現(xiàn)中文亂碼問題解決方法詳解

標簽:宿州 紅河 丹東 云浮 西藏 青島 貴州 江西

巨人網(wǎng)絡通訊聲明:本文標題《JSP中文亂碼常見3個例子及其解決方法》,本文關鍵詞  JSP,中文,亂碼,常見,3個,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《JSP中文亂碼常見3個例子及其解決方法》相關的同類信息!
  • 本頁收集關于JSP中文亂碼常見3個例子及其解決方法的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    和林格尔县| 富裕县| 乌拉特中旗| 定安县| 比如县| 云林县| 贵阳市| 襄汾县| 勐海县| 政和县| 麦盖提县| 恩施市| 温泉县| 香港 | 英山县| 民权县| 洮南市| 开封市| 永平县| 泾阳县| 饶阳县| 高碑店市| 吉林省| 陈巴尔虎旗| 长沙市| 江永县| 桐梓县| 平武县| 安化县| 鹤壁市| 定边县| 西林县| 桦南县| 万源市| 镇江市| 会同县| 游戏| 任丘市| 江源县| 洛阳市| 苍山县|