手机版

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

时间:2021-10-02 来源:互联网 编辑:宝哥软件园 浏览:

常见3个例子及其解决方法如下

实例一、JSP页面显示时

超文本标记语言标题中文乱码——JSP页面显示时/标题/头部身体中心br/h1木兰辞拟古决绝词柬友/h1 p人生若只如初见,何事秋风悲画扇p/p等闲变却故人心,却道故人心易变p/p骊山语罢清宵半,泪雨霖铃终不怨p/p何如薄幸锦衣郎,比翼连枝当日愿/p/中心/正文/html运行结果:

解决方法:为其指定中文字符集,html前加入

"%@"页面内容类型=' text/html;charset=gb2312' %

实例二、JSP页面传递中文参数时

注册页面:

"%@"页面内容类型=' text/html;charset=GB 2312“% html标题"中文乱码——JSP页面传递中文参数时/标题/头体氘申请账号:/h2表单操作=' userMsg.jsp '方法='POST' p邮箱:输入类型=' text ' name=' email ' id=' email '/p/p昵称:输入类型='text'name='昵称id='昵称/p/p密码:输入类型=' password ' name=' password ' id=' password '/p/p性别:输入类型=' radio ' name=' sex ' id=' sex ' value='男'/男输入类型='收音机'名称='sex'id='sex '值='女'/女p/text区域名称=' introduction ' id=' introduction ' rows=' 5 ' cols=' 27 '一句话介绍自己./textarea pinput类型='submit'value='提交申请/p/表单/正文/html个人信息页面:

"%@"页面内容类型=' text/html;charset=GB 2312“% html标题"中文乱码——JSP页面传递中文参数时/标题/头部身体中心氘用户信息:/H2 %字符串电子邮件=请求。GetParameter(' email ');% %字符串昵称=request.getParameter('昵称');% %字符串密码=请求。GetParameter('密码');% %字符串性别=请求。GetParameter(' sex ');% %字符串介绍=请求。GetParameter(' introduction ');% p邮箱:% out.print(电子邮件);%p/p昵称:% out.print(昵称);%p/p密码:% out.print(密码);%p/p性别:% out.print(性别);%p/p个人介绍:%out.print(简介);%/p/中心/正文/html运行结果:

解决方法:修改个人信息页面如下

"%@"页面内容类型=' text/html;charset=GB 2312“% html标题"中文乱码——JSP页面传递中文参数时/标题/头体氘用户信息:/H2 %字符串电子邮件=新闻字符串(请求。GetParameter('电子邮件').getBytes('ISO-8859-1 ',' GB 2312 ');% %字符串昵称=新闻字符串(request.getParameter('昵称')。getBytes('ISO-8859-1 ',' GB 2312 ');% %字符串密码=新闻字符串(请求。GetParameter('密码').getBytes('ISO-8859-1 ',' GB 2312 ');% %字符串性别=新闻字符串(请求。GetParameter(' sex ').getBytes('ISO-8859-1 ',' GB 2312 ');% % String简介=newString(请求。getparameter(' introduction ').getBytes('ISO-8859-1 ',' GB 2312 ');% p邮箱:% out.print(电子邮件);%p/p昵称:% out.print(昵称);%p/p密码:% out.print(密码);%p/p性别:% out.print(性别);%p/p个人介绍:%out.print(简介);%/p /body /html实例三、Servlet处理中文参数时

注册页面:

"%@"页面内容类型=' text/html;charset=GB 2312"% % @页面导入="测试。用户消息% html标题中文乱码——JSP页面传递中文参数时/标题/头体氘申请账号:/h2表单操作='。/UserMsg '方法='POST' p邮箱:输入类型=' text ' name=' email ' id=' email '/p/p昵称:输入类型='text'name='昵称id='昵称/p/p密码:输入类型=' password ' name=' password ' id=' password '/p/p性别:输入类型=' radio ' name=' sex ' id=' sex ' value='男'/男输入类型='收音机'名称='sex'id='sex '值='女'/女p/text区域名称=' introduction ' id=' introduction ' rows=' 5 ' cols=' 27 '一句话介绍自己./textarea pinput类型='submit'value='提交申请/p/form/body/html UserMSg。Java(Servlet)

包装测试;导入Java。io。ioexception导入Java。io。版画作家;导入Java。io。unsupportedencodinegexception导入javax。servlet。http。HttpServlet导入javax。servlet。http。HttpServletrequest导入javax。servlet。http。HttpServletresponse公共类用户消息扩展了httpersvlet { public void doGet(httpersvletrequest,httpersvletresponse response){ doPost(request,response);} public void doPost(HttpServletrequest,HttpServletResponse响应){ try { request。setcharacter编码(' GB 2312 ');} catch(UnSupportedencodinegexception){ e . print stack trace();} PrintWriter out=null尝试{ out=响应。getwriter();} catch(IOException E1){ E1。print stack trace();}出去。打印(' html ');出去。打印(“正文”);输出(h2)用户信息:' '/H2 ');输出(p)邮箱:'请求。getParameter(' email ')' p/');输出(p)昵称:' request.getParameter('昵称)' p/');输出(p)密码:'请求。getParameter(' password ')' p/');输出(p)性别:'请求。GetParameter(' sex ')' p/');输出(p)个人介绍:'请求。GetParameter(' introduction ')' p/');出去。print('/html ');出去。打印('/body ');} } 运行结果:

解决方法:在doPost中加入:

回应。setcontenttype(' text/html;charset=GB 2312’);

以上就是几种常见JSP中文乱码例子及其解决方法,希望能够帮助大家解决JSP中文乱码的问题。

版权声明:JSP中文乱码常见3个例子及其解决方法是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。

相关文章推荐