解决JSP页面显示乱码问题
创始人
2024-04-12 13:11:31
0

一、JSP页面显示乱码

二、表单提交中文时出现乱码

三、数据库连接

大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着您,我现在把我在JSP开发中遇到

的中文乱码的问题及解决办法写出来供大家参考。

一、JSP页面显示乱码
下面的显示页面(display.jsp)就出现乱码:

  1.  
  2.  
  3. </FONT></STRONG>JSP的中文处理<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><meta</FONT></STRONG> <FONT color=#ff0000>http-equiv</FONT>=<FONT color=#0000ff>"Content-Type"</FONT> <FONT color=#ff0000>content</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG>%  <LI class="">out.print("JSP的中文处理");  <LI class=alt>%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>对不同的WEB服务器和不同的JDK版本,处理结果就不一样。原因:服务器使用的编码方式不同和浏览器对不同的字符显示结果不同而导致的。解决办法:在JSP页面中指定编码方式(gb2312),即在页面的***行加上:<%@ page contentType="text/html; charset=gb2312"%>,就可以消除乱码了。完整页面如下</P><PRE><OL class=dp-xml><LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG>%@ page <FONT color=#ff0000>contentType</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT>%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><html></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><head></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><title></FONT></STRONG>JSP的中文处理<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><meta</FONT></STRONG> <FONT color=#ff0000>http-equiv</FONT>=<FONT color=#0000ff>"Content-Type"</FONT> <FONT color=#ff0000>content</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG>%  <LI class=alt>out.print("JSP的中文处理");  <LI class="">%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>二、表单提交中文时出现乱码<BR>下面是一个提交页面(submit.jsp),代码如下:</P><PRE><OL class=dp-xml><LI class=alt><STRONG><FONT color=#006699><html></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><head></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><title></FONT></STRONG>JSP的中文处理<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><meta</FONT></STRONG> <FONT color=#ff0000>http-equiv</FONT>=<FONT color=#0000ff>"Content-Type"</FONT> <FONT color=#ff0000>content</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><form</FONT></STRONG> <FONT color=#ff0000>name</FONT>=<FONT color=#0000ff>"form1"</FONT> <FONT color=#ff0000>method</FONT>=<FONT color=#0000ff>"post"</FONT> <FONT color=#ff0000>action</FONT>=<FONT color=#0000ff>"process.jsp"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><div</FONT></STRONG> <FONT color=#ff0000>align</FONT>=<FONT color=#0000ff>"center"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><input</FONT></STRONG> <FONT color=#ff0000>type</FONT>=<FONT color=#0000ff>"text"</FONT> <FONT color=#ff0000>name</FONT>=<FONT color=#0000ff>"name"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><input</FONT></STRONG> <FONT color=#ff0000>type</FONT>=<FONT color=#0000ff>"submit"</FONT> <FONT color=#ff0000>name</FONT>=<FONT color=#0000ff>"Submit"</FONT> <FONT color=#ff0000>value</FONT>=<FONT color=#0000ff>"Submit"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>div></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>form></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>下面是处理页面(process.jsp)代码:</P><PRE><OL class=dp-xml><LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG>%@ page <FONT color=#ff0000>contentType</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT>%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><html></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><head></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><title></FONT></STRONG>JSP的中文处理<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><meta</FONT></STRONG> <FONT color=#ff0000>http-equiv</FONT>=<FONT color=#0000ff>"Content-Type"</FONT> <FONT color=#ff0000>content</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=alt> <LI class=""><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG>%=request.getParameter("name")%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>如果submit.jsp提交英文字符能正确显示,如果提交中文时就会出现乱码。原因:浏览器默认使用UTF-8编码方式来发送请求,而UTF- 8和GB2312编码方式表示字符时不一样,这样就出现了不能识别字符。解决办法:通过request.seCharacterEncoding ("gb2312")对请求进行统一编码,就实现了中文的正常显示。修改后的process.jsp代码如下:</P><PRE><OL class=dp-xml><LI class=alt><STRONG><FONT color=#006699><</FONT></STRONG>%@ page <FONT color=#ff0000>contentType</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT>%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG>%  <LI class=alt>request.seCharacterEncoding("gb2312");  <LI class="">%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><html></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><head></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699><title></FONT></STRONG>JSP的中文处理<STRONG><FONT color=#006699></< SPAN>title></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><meta</FONT></STRONG> <FONT color=#ff0000>http-equiv</FONT>=<FONT color=#0000ff>"Content-Type"</FONT> <FONT color=#ff0000>content</FONT>=<FONT color=#0000ff>"text/html; charset=gb2312"</FONT><STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>head></FONT></STRONG> <LI class=""> <LI class=alt><STRONG><FONT color=#006699><body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699><</FONT></STRONG>%=request.getParameter("name")%<STRONG><FONT color=#006699>></FONT></STRONG> <LI class=alt><STRONG><FONT color=#006699></< SPAN>body></FONT></STRONG> <LI class=""><STRONG><FONT color=#006699></< SPAN>html></FONT></STRONG> </LI></OL></PRE> <P>三、数据库连接出现乱码<BR>只要涉及中文的地方全部是乱码,解决办法:在数据库的数据库URL中加上<BR>useUnicode=true&characterEncoding=GBK 就OK了。</P> <P>四、数据库的显示乱码<BR>在mysql4.1.0中,varchar类型,text类型就会出现中文乱码,对于varchar类型把它设为binary属性就可以解决JSP页面显示乱码问题了</P> <P>【编辑推荐】</P> <OL> <LI><FONT color=#0000ff>解决Servlet JSP页面乱码问题</FONT></LI> <LI><FONT color=#0000ff>Servlet和JSP的安全问题</FONT></LI> <LI><FONT color=#0000ff>标签库中JSP Servlet调用</FONT></LI> <LI><FONT color=#0000ff>Struts和JSP页面的技术</FONT></LI> <LI><FONT color=#0000ff>详解JSP服务器的安装</FONT></LI></OL> <!--end::Text--> </div> <!--end::Description--> <div class="mt-5"> <!--关键词搜索--> </div> <div class="mt-5"> <p class="fc-show-prev-next"> <strong>上一篇:</strong><a href="/chengxu/14216.html">迁移工厂计划成功帮助Kennametal转向IBM</a><br> </p> <p class="fc-show-prev-next"> <strong>下一篇:</strong><a href="/chengxu/14218.html">F5 Networks添加vSphere与vCenter集成</a> </p> </div> <!--begin::Block--> <div class="d-flex flex-stack mb-2 mt-10"> <!--begin::Title--> <h3 class="text-dark fs-5 fw-bold text-gray-800">相关内容</h3> <!--end::Title--> </div> <div class="separator separator-dashed mb-9"></div> <!--end::Block--> <div class="row g-10"> </div> </div> <!--end::Table widget 14--> </div> <!--end::Col--> <!--begin::Col--> <div class="col-xl-4 mt-0"> <!--begin::Chart Widget 35--> <div class="card card-flush h-md-100"> <!--begin::Header--> <div class="card-header pt-5 "> <!--begin::Title--> <h3 class="card-title align-items-start flex-column"> <!--begin::Statistics--> <div class="d-flex align-items-center mb-2"> <!--begin::Currency--> <span class="fs-5 fw-bold text-gray-800 ">热门资讯</span> <!--end::Currency--> </div> <!--end::Statistics--> </h3> <!--end::Title--> </div> <!--end::Header--> <!--begin::Body--> <div class="card-body pt-3"> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/241137.html" class="text-dark fw-bold text-hover-primary fs-6">如何允许远程连接到MySQL数...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">[[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/61017.html" class="text-dark fw-bold text-hover-primary fs-6">如何利用交换机和端口设置来管理...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/119031.html" class="text-dark fw-bold text-hover-primary fs-6">施耐德电气数据中心整体解决方案...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('https://files.pic99.top/shayuweb/202409/eb2e03366045d9f.jpg')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/139440.html" class="text-dark fw-bold text-hover-primary fs-6">20个非常棒的扁平设计免费资源</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/27164.html" class="text-dark fw-bold text-hover-primary fs-6">2009 IBM动态架构新动力...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7"></span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/225.html" class="text-dark fw-bold text-hover-primary fs-6">通过 XML 进行内容发布</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7"></span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/130901.html" class="text-dark fw-bold text-hover-primary fs-6">德国电信门户网站可实时显示全球...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('https://files.pic99.top/shayuweb/202503/5fe82cca6c5ff78.jpg')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/246646.html" class="text-dark fw-bold text-hover-primary fs-6">为啥国人偏爱 Mybatis,...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('https://files.pic99.top/shayuweb/202407/404021d01b8347f.jpg')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/48727.html" class="text-dark fw-bold text-hover-primary fs-6">《非诚勿扰》红人闫凤娇被曝厕所...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...</span> </div> <!--end::Title--> </div> <!--begin::Item--> <div class="d-flex flex-stack mb-7"> <!--begin::Symbol--> <div class="symbol symbol-60px symbol-2by3 me-4"> <div class="symbol-label" style="background-image: url('/static/assets/images/nopic.gif')"></div> </div> <!--end::Symbol--> <!--begin::Title--> <div class="m-0"> <a href="/chengxu/133934.html" class="text-dark fw-bold text-hover-primary fs-6">2012年第四季度互联网状况报...</a> <span class="text-gray-600 fw-semibold d-block pt-1 fs-7">[[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...</span> </div> <!--end::Title--> </div> </div> <!--end::Body--> </div> <!--end::Chart Widget 35--> </div> <!--end::Col--> </div> </div> <!--end::Content container--> </div> <!--end::Content--> </div> <!--end::Content wrapper--> <!--begin::Footer--> <div id="kt_app_footer" class="app-footer"> <!--begin::Footer container--> <div class="app-container container-xxl d-flex flex-column flex-md-row flex-center flex-md-stack py-3"> <!--begin::Copyright--> <div class="text-dark order-2 order-md-1"> <span class="text-muted fw-semibold me-1">2025 ©</span> <a href="/" target="_blank" class="text-gray-800 text-hover-primary">鲨鱼网</a> <a href="https://beian.miit.gov.cn/" target="_blank" class="text-gray-800 text-hover-primary"></a> <a href="http://spbjmm.com.shayuweb.com">上品网</a><a href="http://www.zzszq.net/">深知网</a><a href="http://www.taiyangwa.net/">太阳生活网</a><a href="http://baike.taiyangwa.net/">太阳百科网</a><a href="http://ypkjmy.com.shayuweb.com/">一品科技</a><a href="http://www.yuansudz.com/news/">元素网</a><a href="http://www.xn--i6qw12a.com/">帛典网</a><a href="http://xldmm.com.shayuweb.com/">星链岛</a> </div> <!--end::Copyright--> <!--begin::Menu--> <ul class="menu menu-gray-600 menu-hover-primary fw-semibold order-1"> <li class="menu-item"> <a href="/news/" target="_blank" class="menu-link px-2">科技资讯</a> </li> <li class="menu-item"> <a href="/chengxu/" target="_blank" class="menu-link px-2">程序开发</a> </li> <li class="menu-item"> <a href="/sitemap.xml" target="_blank" class="menu-link px-2">sitemap</a> </li> </ul> <!--end::Menu--> </div> <!--end::Footer container--> </div> <!--end::Footer--> </div> <!--end:::Main--> </div> <!--end::Wrapper--> </div> <!--end::Page--> </div> <!--end::App--> <div id="kt_scrolltop" class="scrolltop" data-kt-scrolltop="true"> <!--begin::Svg Icon | path: icons/duotune/arrows/arr066.svg--> <span class="svg-icon"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <rect opacity="0.5" x="13" y="6" width="13" height="2" rx="1" transform="rotate(90 13 6)" fill="currentColor"></rect> <path d="M12.5657 8.56569L16.75 12.75C17.1642 13.1642 17.8358 13.1642 18.25 12.75C18.6642 12.3358 18.6642 11.6642 18.25 11.25L12.7071 5.70711C12.3166 5.31658 11.6834 5.31658 11.2929 5.70711L5.75 11.25C5.33579 11.6642 5.33579 12.3358 5.75 12.75C6.16421 13.1642 6.83579 13.1642 7.25 12.75L11.4343 8.56569C11.7467 8.25327 12.2533 8.25327 12.5657 8.56569Z" fill="currentColor"></path> </svg> </span> <!--end::Svg Icon--> </div> <!--begin::Javascript--> <script>var hostUrl = "/static/default/pc/";</script> <!--begin::Global Javascript Bundle(mandatory for all pages)--> <script src="/static/default/pc/plugins/global/plugins.bundle.js"></script> <script src="/static/default/pc/js/scripts.bundle.js"></script> <!--end::Global Javascript Bundle--> <!--end::Javascript--> </body> <!--end::Body--> </html>