Java web 开发中文乱码问题

一.Eclipse下编码的设定(在myeclipse下设定方法类似)

1、window -> Preferences -> general -> Workspace -> Text file encoding(这里是设置工作去的编码)

选择Other,将编码改变为UTF-8,这样以后新建立工程其属性对话框中的Text file encoding即为UTF-8。

2、window -> Preferences -> general -> Content Types(这里是设置不同文件的编码,例如java文件,jsp文件,css文件等不同类型的文件的编码方式)

例如:选择Text树,点开,选择Java Source File,在下面的Default encoding输入框中输入UTF-8,按下Update按钮,则设置Java文件编码为UTF-8。

设置其他文件类型的编码方式也是在这里进行设定。最好统一设定文件格式为utf-8


二.jsp页面编码解码方式的设定

[html]  view plain copy
  1. <span style="color:#333333;"><%@ page language="java" contentType="text/html; charset=utf-8"  
  2.     pageEncoding="utf-8"%>  
  3. <html>  
  4. <head>  
  5. <title>中文问题</title>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. </head>  
  8. <body>  
  9. 中文乱码问题  
  10. </body>  
  11. </html>  
  12.   
  13.   
  14. </span>  
第一个地方的编码格式为jsp文件的存储格式。Eclipse会根据这个编码格式保存文件。并编译jsp文件,包括里面的汉字。
第二处编码为解码格式。因为存为UTF-8的文件被解码(
服务器发送给客户端 )为iso8859-1,这样 如有中文肯定出乱码。也就是必须一致。

第三处编码为控制浏览器的解码方式。如果前面的解码都一致并且无误的话,这个编码格式没有关系。有的网页出现乱码,就是因为浏览器不能确定使用哪种编码格式。因为页面有时候会嵌入页面,导致浏览器混淆了编码格式。出现了乱码。

pageEncoding和charset的区别

pageEncoding=“utf-8”是jsp文件本身的编码
<pre id="best-answer-content" class="reply-text mb10" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; background-color: rgb(255, 252, 246); padding: 0px; font-family: Arial; zoom: 1;"><span style="line-height: 26px;">contentType="text/html;charset=UTF-8"</span>的charset是指服务器发送给客户端时的内容编码
JSP要经过两次的“编码”,第一阶段会用pageEncoding,第二阶段会用utf-8至utf-8,第三阶段就是由Tomcat出来的网页, 用的是contentType。第一阶段是jsp编译成.java,它会根据pageEncoding的设定读取jsp,结果是由指定的编码方案翻译成统一的UTF-8 JAVA源码(即.java),如果pageEncoding设定错了,或没有设定,出来的就是中文乱码。第二阶段是由JAVAC的JAVA源码至java byteCode的编译,不论JSP编写时候用的是什么编码方案,经过这个阶段的结果全部是UTF-8的encoding的java源码。
 
<strong>三.表单方式中文乱码解决方案</strong>
<span style="line-height: 26px;">1.表单使用Post方式提交后接收到的乱码问题</span><br style="line-height: 26px;" /><span style="line-height: 26px;">这个问题是一个常见的问题。这个乱码也是tomcat的内部编码格式iso8859-1在捣乱,也就是说post提交时,如果没有设置提交的编码格式,则会以iso8859-1方式进行提交,接受的jsp却以utf-8的方式接受。导致乱码。既然这样的原因,下面有几种解决方式,并比较。</span><br style="line-height: 26px;" /><br style="line-height: 26px;" /><span style="line-height: 26px;">(1).接受参数时进行编码转换</span><br style="line-height: 26px;" /><br style="line-height: 26px;" /><span style="line-height: 26px;">String str = new String(request.getParameter("something").getBytes("ISO-8859-1"),"utf-8") ; 这样的话,每一个参数都必须这样进行转码。很麻烦。但确实可以拿到汉字。</span><br style="line-height: 26px;" /><br style="line-height: 26px;" /><span style="line-height: 26px;">(2)在请求页面上开始处,执行请求的编码代码, request.setCharacterEncoding("UTF-8"),把提交内容的字符集设为UTF-8。这样的话,接受此参数的页面就不必在转码了。直接使用</span><br style="line-height: 26px;" /><br style="line-height: 26px;" /><span style="line-height: 26px;">String str = request.getParameter("something");即可得到汉字参数。但每页都需要执行这句话。这个方法也就对post提交的有效果,对于get提交和上传文件时的enctype="multipart/form-data"是无效的。稍后下面单独对这个两个的乱码情况再进行说明。</span><br style="line-height: 26px;" /><br style="line-height: 26px;" /><span style="line-height: 26px;">(3).为了避免每页都要写request.setCharacterEncoding("UTF-8"),建议使用过滤器对所有jsp</span><br style="line-height: 26px;" /><br style="line-height: 26px;" /><span style="line-height: 26px;">进行编码处理。</span>
<span style="line-height: 26px;">2. 表单get提交方式的乱码处理方式。

如果使用get方式提交中文,接受参数的页面也会出现乱码,这个乱码的原因也是tomcat的内部编码格式iso8859-1导致。Tomcat会以get的缺省编码方式iso8859-1对汉字进行编码,编码后追加到url,导致接受页面得到的参数为乱码/、。

解决办法:
(1) 使用上例中的第一种方式,对接受到的字符进行解码,再转码。

(2)Get走的是url提交,而在进入url之前已经进行了iso8859-1的编码处理。要想影响这个编码则需要在server.xml的Connector节点增加useBodyEncodingForURI="true" 

属性配置,即可控制tomcat对get方式的汉字编码方式,上面这个属性控制get提交也是用request.setCharacterEncoding("UTF-8")所设置的编码格式进行编码。所以自动编码为utf-8,接受页面正常接受就可以了。但我认为真正的编码过程是,tomcat又要根据

<Connector port="8080" 

maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 

enableLookups="false" redirectPort="8443" acceptCount="100" 

debug="0" connectionTimeout="20000" useBodyEncodingForURI="true" 

disableUploadTimeout="true" URIEncoding=”UTF-8”/>
(<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.200000762939453px;">在conf目录下的server.xml 中 </span>)
里面所设置的URIEncoding=”UTF-8”再进行一次编码,但是由于已经编码为utf-8,再编码也不会有变化了。如果是从url获取编码,接受页面则是根据URIEncoding=”UTF-8”来进行解码的。</span>
 
 
 
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">在java开发中,经常会遇到乱码问题(包括中文乱码、其他国家语言文字乱码等),怎么样才能花最小的代价来解决这个问题呢?下面就将提供一种方式来彻底解决这个麻烦。(本方法已经在很多项目中使用,希望能给那些为乱码困扰的朋友带来帮助!) </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">方法分成以下3个步骤(请放心,每一步都非常简单) </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">1. 首先将开发项目(如在Eclipse中创建的项目)的设置成UTF-8编码方式(如下图)。这一点在开始一个新的项目的时候尤其重要,目前的项目基本上都使用UTF-8编码了。 </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><img src="http://stephen830.iteye.com/upload/attachment/43174/110a35bb-4688-36bd-8792-9067234aedb0.jpg" style="border: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" alt="" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;"> </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">2. 确保项目内的所有开发文件都是UTF-8编码的。这里的开发文件主要指:java,jsp,html,js,css,xml类型等开发涉及到的文件,当然图片文件就不用了,貌似没有UTF-8的图形文件。其中的java,js,css,xml类型文件,只要文件的编码方式为UTF-8就可以了。而jsp文件则还需要在文件内容中设置如下: </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div class="bar"><div class="tools" style="padding: 3px; margin: 0px; font-weight: bold;">Html代码  <a target=_blank title="收藏这段代码" style="color: rgb(16, 138, 198); text-decoration: underline;"><img class="star" src="http://stephen830.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" class="dp-xml" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span>%@ page <span class="attribute" style="color: red;">contentType</span>=<span class="attribute-value" style="color: blue;">"text/html;charset=UTF-8"</span>%<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">...  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">meta</span> <span class="attribute" style="color: red;">http-equiv</span>=<span class="attribute-value" style="color: blue;">"Content-Type"</span> <span class="attribute" style="color: red;">content</span>=<span class="attribute-value" style="color: blue;">"text/html; charset=UTF-8"</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">...  </span></li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">另外html文件还需要在文件内容中设置: </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div class="bar"><div class="tools" style="padding: 3px; margin: 0px; font-weight: bold;">Html代码  <a target=_blank title="收藏这段代码" style="color: rgb(16, 138, 198); text-decoration: underline;"><img class="star" src="http://stephen830.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" class="dp-xml" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">...  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">meta</span> <span class="attribute" style="color: red;">http-equiv</span>=<span class="attribute-value" style="color: blue;">"Content-Type"</span> <span class="attribute" style="color: red;">content</span>=<span class="attribute-value" style="color: blue;">"text/html; charset=UTF-8"</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">...  </span></li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">对jsp文件和html文件要特别注意,大多数时候遇到乱码是因为粗心没有进行UTF-8编码或者文件内容没有设置对造成的。遇到乱码问题一般先检查相应的文件编码和内容是否符合要求。 </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">如果原来不是UTF-8编码的可以用文本工具进行转换,包括最简单的记事本也可以将打开的文件另存为UTF-8编码,当然可以用UltraEdit工具转换[菜单:文件-转换-ASCII转UTF-8]更加方便。 </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">3. 在项目应用中加一个编码虑镜。编码虑镜的代码(很简单的)具体如下: </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div class="bar"><div class="tools" style="padding: 3px; margin: 0px; font-weight: bold;">Java代码  <a target=_blank title="收藏这段代码" style="color: rgb(16, 138, 198); text-decoration: underline;"><img class="star" src="http://stephen830.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" class="dp-j" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">/*</span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;"> * Created on 2005-11-6</span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;"> * Author stephen</span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;"> * Email zhoujianqiang AT gmail DOT com</span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;"> * CopyRight(C)2005-2008 , All rights reserved.</span> </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;"> */</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">package</span> com.soft4j.filter;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> java.io.IOException;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">import</span> javax.servlet.*;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">class</span> CharsetFilter <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">implements</span> Filter {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">protected</span> String encoding = <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span>;<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">// ///要制定的编码,在web.xml中配置</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">protected</span> FilterConfig filterConfig = <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span>;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span> destroy() {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span>.encoding = <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span>;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span>.filterConfig = <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span>;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    }  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span> doFilter(ServletRequest request, ServletResponse response,  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">            FilterChain chain) <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span> IOException, ServletException {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">if</span> (request.getCharacterEncoding() == <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span>) {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">            String encoding = getEncoding();<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">// //得到指定的编码名字</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">            <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">if</span> (encoding != <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">null</span>)  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">                request.setCharacterEncoding(encoding);<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">// //设置request的编码</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        }  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        chain.doFilter(request, response);<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">// /有机会执行下一个filter</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    }  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">public</span> <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">void</span> init(FilterConfig filterConfig) <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">throws</span> ServletException {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span>.filterConfig = filterConfig;  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span>.encoding = filterConfig.getInitParameter(<span class="string" style="color: blue;">"encoding"</span>);<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">// /得到在web.xml中配置的编码</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    }  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">protected</span> String getEncoding() {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">return</span> (<span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">this</span>.encoding);<span class="comment" style="color: rgb(0, 130, 0); padding: 0px; margin: 0px; width: auto; border: 0px;">// /得到指定的编码</span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    }  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">}  </span></li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">编码虑镜的使用方法:只要在项目的web.xml文件的开头中加入如下的配置参数就可以了: </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div class="bar"><div class="tools" style="padding: 3px; margin: 0px; font-weight: bold;">Xml代码  <a target=_blank title="收藏这段代码" style="color: rgb(16, 138, 198); text-decoration: underline;"><img class="star" src="http://stephen830.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" class="dp-xml" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; color: rgb(43, 145, 175);"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-name</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>SetCharacterEncoding<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-name</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-class</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>com.soft4j.filter.CharsetFilter<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-class</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">init-param</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">param-name</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>encoding<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">param-name</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">param-value</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>UTF-8<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">param-value</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">init-param</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-mapping</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-name</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>SetCharacterEncoding<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-name</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">url-pattern</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>/*<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">url-pattern</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">filter-mapping</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">这样能确保所有出入项目的调用都是UTF-8编码的。 </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px;">经过上面的3个步骤后,你的java项目就不必再受到乱码的困扰了,当然在实际的开发中,由于使用一些第3方的开源组件可能会遇到乱码问题,不过,大部分的问题都是由于开源组件造成的,不必怀疑自己的项目。只要对开源组件中的编码方式进行设置或者作相应的修改,一般就能解决问题。</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值