欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP中文乱码常见3个例子及其解决方法

程序员文章站 2022-05-18 12:52:16
常见3个例子及其解决方法如下 实例一、jsp页面显示时 中文乱...</div> <div class="content"> <p><strong>常见3个例子及其解决方法如下</strong></p> <p><strong>实例一、jsp页面显示时</strong></p> <div class="jb51code"> <pre class="brush: java;"> <html> <head> <title>中文乱码——jsp页面显示时</title> </head> <body> <center> <br/> <h1>木兰辞拟古决绝词柬友</h1> <p>人生若只如初见,何事秋风悲画扇。</p> <p>等闲变却故人心,却道故人心易变。</p> <p>骊山语罢清宵半,泪雨霖铃终不怨。</p> <p>何如薄幸锦衣郎,比翼连枝当日愿。</p> </center> </body> </html></pre> </div> <p>运行结果:</p> <p style="text-align: center"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"id="theimg" onclick="window.open(this.src)" src="/default/index/img?u=aHR0cDovL2ltYWdlczQuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAxOTA1MjgvYl8wXzIwMTkwNTI4MTgwMzMzMTUwNi5qcGc=" baiduimageplusstatus="2" baiduimageplusrect="null" alt="JSP中文乱码常见3个例子及其解决方法" title="JSP中文乱码常见3个例子及其解决方法"></p> <p><strong><span style="color: #ff0000">解决方法:</span></strong>为其指定中文字符集,<html>前加入</p> <p><%@ page contenttype="text/html;charset=gb2312" %> <br></p> <p><strong>实例二、jsp页面传递中文参数时</strong></p> <p>注册页面:</p> <div class="jb51code"> <pre class="brush: java;"> <%@ 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" 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> </pre> </div> <p><strong>个人信息页面:</strong></p> <div class="jb51code"> <pre class="brush: java;"> <%@ 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> </pre> </div> <p>运行结果:</p> <p style="text-align: center"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"id="theimg" onclick="window.open(this.src)" src="/default/index/img?u=aHR0cDovL2ltYWdlczQuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAxOTA1MjgvYl8wXzIwMTkwNTI4MTgwMzMzODU5MS5qcGc=" baiduimageplusstatus="2" baiduimageplusrect="null" alt="JSP中文乱码常见3个例子及其解决方法" title="JSP中文乱码常见3个例子及其解决方法"></p> <p><strong><span style="color: #ff0000">解决方法:</span></strong>修改个人信息页面如下</p> <div class="jb51code"> <pre class="brush: java;"> <%@ 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> </pre> </div> <p><strong>实例三、servlet处理中文参数时</strong></p> <p><strong>注册页面:</strong></p> <div class="jb51code"> <pre class="brush: java;"> <%@ 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" 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> </pre> </div> <p><strong>usermsg.java(servlet)</strong></p> <div class="jb51code"> <pre class="brush: java;"> 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>"); } } </pre> </div> <p>运行结果:</p> <p style="text-align: center"><img onerror="this.src='/statics/superweb999/images/image_error.jpg'"id="theimg" onclick="window.open(this.src)" src="/default/index/img?u=aHR0cDovL2ltYWdlczQuMTBxaWFud2FuLmNvbS8xMHFpYW53YW4vMjAxOTA1MjgvYl8wXzIwMTkwNTI4MTgwMzMzNDg5MS5qcGc=" baiduimageplusstatus="2" baiduimageplusrect="null" alt="JSP中文乱码常见3个例子及其解决方法" title="JSP中文乱码常见3个例子及其解决方法"></p> <p><span style="color: #ff0000"><strong>解决方法:</strong></span>在dopost中加入:</p> <p>response.setcontenttype("text/html; charset=gb2312");</p> <p>以上就是几种常见jsp中文乱码例子及其解决方法,希望能够帮助大家解决jsp中文乱码的问题。</p> </div> <div class="info-pre-next"> <p> 上一篇: <a href="/article/1037641.html"> JSP入门教程之客户端验证、常用输出方式及JSTL基本用法 </a> </p> <p> 下一篇: <a href="/article/1037643.html"> 小米手环2怎么管理身体? </a> </p> </div> <div class="wz_tuijian"> <p> 推荐阅读 </p> <ul> <li> <a href="/article/1898680.html" target="_blank" title="JSP中文乱码常见3个例子及其解决方法"> <h2> JSP中文乱码常见3个例子及其解决方法 </h2> </a> </li> <li> <a href="/article/1037642.html" target="_blank" title="JSP中文乱码常见3个例子及其解决方法"> <h2> JSP中文乱码常见3个例子及其解决方法 </h2> </a> </li> </ul> </div> </article> </div> </main> <footer><div class="box"><div class="ft_nav"><div class="ft_about"><p>关于网站</p><ul><li><a href="/sitemap.xml" target="_blank" title="网站地图">网站地图</a></li><li><a href="/list/2/" title="最新程序员文章站">最新程序员文章站</a></li></ul></div><div class="ft_contact"><ul><li>本站所有数据收集于网络如有侵犯到您的权益,请联系我们进行下架处理。</li><li class="email_show"></li></ul></div></div><div class="copyright"><div class="cr_left"><p> 备案号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤ICP备20058927号</a></p><p>© Copyright © 2020-2022 www.superweb999.com 程序员文章站. </p></div></div></div></footer> </body> </html>