常见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页面传递中文参数时
注册页面:
<%@ page contentType="text/html;charset=gb2312" %> <html> <head> <title>中文乱码——JSP页面传递中文参数时</title> </head> <body> <h2>申请账号:</h2> <form action="userMsg.jsp" method="POST"> <p>邮箱: <input type="text"name="email"/><p/> <p>昵称: <input type="text"name="nickname"/><p/> <p>密码: <input type="password"name="password"/><p/> <p>性别: <input type="radio"name="sex"value="男" /> 男 <input type="radio"id="sex" value="女" /> 女<p/> <textareaid="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页面传递中文参数时 </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>个人介绍:<%out.print(introduction); %></p> </center> </body> </html>
运行结果:
解决方法:修改个人信息页面如下
<%@ page contentType="text/html;charset=gb2312" %> <html> <head> <title>中文乱码——JSP页面传递中文参数时 </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处理中文参数时
注册页面:
<%@ page contentType="text/html;charset=gb2312" %> <%@ page import="test.UserMsg"%> <html> <head> <title>中文乱码——JSP页面传递中文参数时</title> </head> <body> <h2>申请账号:</h2> <form action="./UserMsg" method="POST"> <p>邮箱: <input type="text"name="email"/><p/> <p>昵称: <input type="text"name="nickname"/><p/> <p>密码: <input type="password"name="password"/><p/> <p>性别: <input type="radio"name="sex"value="男" /> 男 <input type="radio"id="sex" value="女" /> 女<p/> <textareaid="introduction" rows="5" cols="27">一句话介绍自己...</textarea> <p><input type="submit"value="提交申请"></p> </form> </body> </html>
UserMsg.java(Servlet)