JSP中文亂碼常見3個(gè)例子及其解決方法
常見3個(gè)例子及其解決方法如下
實(shí)例一、JSP頁(yè)面顯示時(shí)
<html>
<head>
<title>中文亂碼——JSP頁(yè)面顯示時(shí)</title>
</head>
<body>
<center>
<br/>
<h1>木蘭辭擬古決絕詞柬友</h1>
<p>人生若只如初見,何事秋風(fēng)悲畫扇。</p>
<p>等閑變卻故人心,卻道故人心易變。</p>
<p>驪山語(yǔ)罷清宵半,淚雨霖鈴終不怨。</p>
<p>何如薄幸錦衣郎,比翼連枝當(dāng)日愿。</p>
</center>
</body>
</html>
運(yùn)行結(jié)果:

解決方法:為其指定中文字符集,<html>前加入
<%@ page contentType="text/html;charset=gb2312" %>
實(shí)例二、JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí)
注冊(cè)頁(yè)面:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí)</title>
</head>
<body>
<h2>申請(qǐng)賬號(hào):</h2>
<form action="userMsg.jsp" 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="提交申請(qǐng)"></p>
</form>
</body>
</html>
個(gè)人信息頁(yè)面:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(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>郵箱: <% out.print(email); %><p/>
<p>昵稱: <% out.print(nickname); %><p/>
<p>密碼: <% out.print(password); %><p/>
<p>性別: <% out.print(sex); %><p/>
<p>個(gè)人介紹:<%out.print(introduction); %></p>
</center>
</body>
</html>
運(yùn)行結(jié)果:

解決方法:修改個(gè)人信息頁(yè)面如下
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(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>個(gè)人介紹:<%out.print(introduction); %></p>
</body>
</html>
實(shí)例三、Servlet處理中文參數(shù)時(shí)
注冊(cè)頁(yè)面:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="test.UserMsg"%>
<html>
<head>
<title>中文亂碼——JSP頁(yè)面?zhèn)鬟f中文參數(shù)時(shí)</title>
</head>
<body>
<h2>申請(qǐng)賬號(hào):</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="提交申請(qǐng)"></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>"+"個(gè)人介紹:"+request.getParameter("introduction")+"<p/>");
out.print("</html>");
out.print("</body>");
}
}
運(yùn)行結(jié)果:

解決方法:在doPost中加入:
response.setContentType("text/html; charset=gb2312");
以上就是幾種常見JSP中文亂碼例子及其解決方法,希望能夠幫助大家解決JSP中文亂碼的問(wèn)題。
相關(guān)文章
JSP 自定義標(biāo)簽之一 簡(jiǎn)單實(shí)例
當(dāng)前軟件開發(fā)越來(lái)越重視分工與協(xié)作,對(duì)于JSP技術(shù)而言,視圖層與模型層分別由不同的團(tuán)隊(duì)完成也成為理所當(dāng)然的選擇,基于技術(shù)專長(zhǎng)不同及其它因素考慮,眾多規(guī)范中都明確要求JSP頁(yè)面代碼中不允許出現(xiàn)Java代碼。2009-07-07
建立JSP操作以提高數(shù)據(jù)庫(kù)訪問(wèn)的效率
建立JSP操作以提高數(shù)據(jù)庫(kù)訪問(wèn)的效率...2006-10-10

