JSP开发应用是中文乱码是个比较常见的问题其根源是Web容器默认的字符处理编码是ISO
实例一JSP页面显示时
- <html>
- <head>
- <title>中文乱码——JSP页面显示时</title>
- </head>
- <body>
- <center>
- <br/>
- <h>木兰辞拟古决绝词柬友</h>
- <p>人生若只如初见何事秋风悲画扇</p>
- <p>等闲变却故人心却道故人心易变</p>
- <p>骊山语罢清宵半泪雨霖铃终不怨</p>
- <p>何如薄幸锦衣郎比翼连枝当日愿</p>
- </center>
- </body>
- </html>
运行结果
解决方法为其指定中文字符集<html>前加入
- <%@pagecontentType="text/html;charset=gb"%>
实例二JSP页面传递中文参数时
注册页面
- <%@pagecontentType="text/html;charset=gb"%>
- <html>
- <head>
- <title>中文乱码——JSP页面传递中文参数时</title>
- </head>
- <body>
- <h>申请账号</h>
- <formaction="userMsgjsp"method="POST">
- <p>邮箱 <inputtype="text"name="email"id="email"/><p/>
- <p>昵称 <inputtype="text"name="nickname"id="nickname"/><p/>
- &n
bsp;<p>密码 <inputtype="password"name="password"id="password"/><p/>
- <p>性别 <inputtype="radio"name="sex"id="sex"value="男"/>男
- <inputtype="radio"name="sex"id="sex"value="女"/>女<p/>
- <textareaname="introduction"id="introduction"rows=""cols="">一句话介绍自己</textarea>
- <p><inputtype="submit"value="提交申请"></p>
- </form>
- </body>
- </html>
个人信息页面
- <%@pagecontentType="text/html;charset=gb"%>
- <html>
- <head>
- <title>中文乱码——JSP页面传递中文参数时</title>
- </head>
- <body>
- <center>
- <h>用户信息</h>
- <%Stringemail=requestgetParameter("email");%>
- <%Stringnickname=requestgetParameter("nickname");%>
- <%Stringpassword=requestgetParameter("password");%>
- <%Stringsex=requestgetParameter("sex");%>
- <%Stringintroduction=requestgetParameter("introduction");%>
- <p>邮箱 <
;%outprint(email);%><p/>
- <p>昵称 <%outprint(nickname);%><p/>
- <p>密码 <%outprint(password);%><p/>
- <p>性别 <%outprint(sex);%><p/>
- <p>个人介绍<%outprint(introduction);%></p>
- </center>
- </body>
- </html>
运行结果
解决方法修改个人信息页面如下
- <%@pagecontentType="text/html;charset=gb"%>
- <html>
- <head>
- <title>中文乱码——JSP页面传递中文参数时</title>
- </head>
- <body>
- <h>用户信息</h>
- <%Stringemail=newString(requestgetParameter("email")getBytes("ISO")"gb");%>
- <%Stringnickname=newString(requestgetParameter("nickname")getBytes("ISO")"gb");%>
- <%Stringpassword=newString(requestgetParameter("password")getBytes("ISO")"gb");%>
- <%Stringsex=newString(requestgetParameter("sex")getBytes("ISO")"gb");;%>
- <%Stringintroduction=newString(requestgetParameter("introduction")getBytes("ISO")"gb");;%>
- <p>邮箱<%outprint(email);%><p/>
- &
lt;p>昵称<%outprint(nickname);%><p/>
- <p>密码<%outprint(password);%><p/>
- <p>性别<%outprint(sex);%><p/>
- <p>个人介绍<%outprint(introduction);%></p>
- </body>
- </html>
实例三Servlet处理中文参数时
注册页面
- <%@pagecontentType="text/html;charset=gb"%>
- <%@pageimport="testUserMsg"%>
- <html>
- <head>
- <title>中文乱码——JSP页面传递中文参数时</title>
- </head>
- <body>
- <h>申请账号</h>
- <formaction="/UserMsg"method="POST">
- <p>邮箱<inputtype="text"name="email"id="email"/><p/>
- <p>昵称<inputtype="text"name="nickname"id="nickname"/><p/>
- <p>密码<inputtype="password"name="password"id="password"/><p/>
- <p>性别<inputtype="radio"name="sex"id="sex"value="男"/>男
- <inputtype="radio"name="sex"id="sex"value="女"/>女<p/>
- <textareaname="introduction"id="introduction"rows=""cols="">一句话介绍自己</textarea>
- <p><inputtype="submit"value="提交申请"></p>
- &nb
sp;</form>
UserMsgjava(Servlet)
- packagetest;
-
- importjavaioIOException;
- importjavaioPrintWriter;
- importjavaioUnsupportedEncodingException;
-
- importjavaxservlet
- importjavaxservlet
- importjavaxservlet
- publicclassUserMsgextendsHttpServlet{
- publicvoiddoGet(HttpServletRequestrequest
- HttpServletResponseresponse){
- doPost(requestresponse);
- }
- publicvoiddoPost(HttpServletRequestrequest
- HttpServletResponseresponse){
- try{
- requestsetCharacterEncoding("gb");
- }catch(UnsupportedEncodingExceptione){
- eprintStackTrace();
- }
- PrintWriterout=null;
- try{
- out=responsegetWriter();
- }catch(IOExceptione){
- eprintStackTrace();
- }
- outprint("<html>");
- &nbs
p;outprint("<body>");
- outprint("<h>"+"用户信息"+"</h>");
- outprint("<p>"+"邮箱"+requestgetParameter("email")+"<p/>");
- outprint("<p>"+"昵称"+requestgetParameter("nickname")+"<p/>");
- outprint("<p>"+"密码"+requestgetParameter("password")+"<p/>");
- outprint("<p>"+"性别"+requestgetParameter("sex")+"<p/>");
- outprint("<p>"+"个人介绍"+requestgetParameter("introduction")+"<p/>");
- outprint("</html>");
- outprint("</body>");
- }
- }
运行结果
解决方法在doPost中加入
- responsesetContentType("text/html;charset=gb");