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

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的泰安網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
實(shí)例一、JSP頁(yè)面顯示時(shí)
中文亂碼——JSP頁(yè)面顯示時(shí)
木蘭辭擬古決絕詞柬友
人生若只如初見(jiàn),何事秋風(fēng)悲畫(huà)扇。
等閑變卻故人心,卻道故人心易變。
驪山語(yǔ)罷清宵半,淚雨霖鈴終不怨。
何如薄幸錦衣郎,比翼連枝當(dāng)日愿。
運(yùn)行結(jié)果:
解決方法:為其指定中文字符集,前加入
- <%@ page contentType="text/html;charset=gb2312" %>
實(shí)例二、JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí)
注冊(cè)頁(yè)面:
- <%@ page contentType="text/html;charset=gb2312" %>
中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí) 申請(qǐng)賬號(hào):
郵箱:
昵稱:
密碼:
性別: 男
- 女
個(gè)人信息頁(yè)面:
- <%@ page contentType="text/html;charset=gb2312" %>
中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí) 用戶信息:
- <% 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");%>
郵箱: <% out.print(email); %>
昵稱: <% out.print(nickname); %>
密碼: <% out.print(password); %>
性別: <% out.print(sex); %>
個(gè)人介紹:<%out.print(introduction); %>
運(yùn)行結(jié)果:
解決方法:修改個(gè)人信息頁(yè)面如下
- <%@ page contentType="text/html;charset=gb2312" %>
中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí) 用戶信息:
- <% 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");;%>
郵箱: <% out.print(email); %>
昵稱: <% out.print(nickname); %>
密碼: <% out.print(password); %>
性別: <% out.print(sex); %>
個(gè)人介紹:<%out.print(introduction); %>
實(shí)例三、Servlet處理中文參數(shù)時(shí)
注冊(cè)頁(yè)面:
- <%@ page contentType="text/html;charset=gb2312" %>
- <%@ page import="test.UserMsg"%>
中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí) 申請(qǐng)賬號(hào):
郵箱:
昵稱:
密碼:
性別: 男
- 女
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("");
- out.print("");
- out.print("
" +"用戶信息:"+ "
");- out.print("
"+"郵箱:"+request.getParameter("email")+"
");- out.print("
"+"昵稱:"+request.getParameter("nickname")+"
");- out.print("
"+"密碼:"+request.getParameter("password")+"
");- out.print("
"+"性別:"+request.getParameter("sex")+"
");- out.print("
"+"個(gè)人介紹:"+request.getParameter("introduction")+"
");- out.print("");
- out.print("");
- }
- }
運(yùn)行結(jié)果:
解決方法:在doPost中加入:
- response.setContentType("text/html; charset=gb2312");
當(dāng)前名稱:常見(jiàn)JSP中文亂碼例子及其解決方法
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/dhosjoo.html


咨詢
建站咨詢
