<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/chinese-garbled-filter-solution/</link>
<title><![CDATA[解决中文乱码问题，过滤器配置，get post提交乱码，filter,struts乱码，jsp乱码]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[Web开发]]></category>
<pubDate>Thu, 09 Sep 2010 17:08:29 +0000</pubDate> 
<guid>https://www.heckjj.com/chinese-garbled-filter-solution/</guid> 
<description>
<![CDATA[ 
	过滤器配置<br/><textarea name="code" class="xml" rows="15" cols="100"><filter>&nbsp;&nbsp;
 <filter-name>encoding</filter-name>&nbsp;&nbsp;
 <filter-class>&nbsp;&nbsp;
&nbsp;&nbsp;filter.FilterEncoding&nbsp;&nbsp; 
 </filter-class>&nbsp;&nbsp;
 <init-param>&nbsp;&nbsp;
&nbsp;&nbsp;<param-name>encoding</param-name>&nbsp;&nbsp;
&nbsp;&nbsp;<param-value>gbk</param-value>&nbsp;&nbsp;
 </init-param>&nbsp;&nbsp;
</filter></textarea><br/>过滤器java文件：<br/><textarea name="code" class="java" rows="15" cols="100">
package filter;&nbsp;&nbsp; 
&nbsp;&nbsp;
import java.io.IOException;&nbsp;&nbsp;&nbsp;&nbsp;
import javax.servlet.Filter;&nbsp;&nbsp;&nbsp;&nbsp;
import javax.servlet.FilterChain;&nbsp;&nbsp;&nbsp;&nbsp;
import javax.servlet.FilterConfig;&nbsp;&nbsp;&nbsp;&nbsp;
import javax.servlet.ServletException;&nbsp;&nbsp;&nbsp;&nbsp;
import javax.servlet.ServletRequest;&nbsp;&nbsp;&nbsp;&nbsp;
import javax.servlet.ServletResponse;&nbsp;&nbsp;&nbsp;&nbsp;
public class FilterEncoding implements Filter&nbsp;&nbsp;&nbsp;&nbsp;
&#123;&nbsp;&nbsp;&nbsp;&nbsp;
 protected String encoding;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 接收字符编码&nbsp;&nbsp; 
 protected boolean ignore;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 是否忽略大小写&nbsp;&nbsp; 
 protected FilterConfig filterConfig; // 初始化配置&nbsp;&nbsp; 
&nbsp;&nbsp;
 public void init(FilterConfig filterConfig) throws ServletException&nbsp;&nbsp;&nbsp;&nbsp;
 &#123;&nbsp;&nbsp; 
&nbsp;&nbsp;// 从web.xml文件中读取encoding的值&nbsp;&nbsp; 
&nbsp;&nbsp;encoding = filterConfig.getInitParameter("encoding");&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;// 从web.xml文件中读取ignore的值&nbsp;&nbsp; 
&nbsp;&nbsp;String value = filterConfig.getInitParameter("ignore");&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;// 以下三种情况均为忽略大小写&nbsp;&nbsp; 
&nbsp;&nbsp;if(value == null)&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; ignore = true;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;&nbsp;&nbsp; 
&nbsp;&nbsp;else if(value.equalsIgnoreCase("yes"))&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; ignore = true;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;else if(value.equalsIgnoreCase("true"))&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; ignore = true;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;&nbsp;&nbsp;&nbsp;&nbsp;
 &#125;&nbsp;&nbsp;&nbsp;&nbsp;
 // doFilter方法&nbsp;&nbsp; 
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException&nbsp;&nbsp;&nbsp;&nbsp;
 &#123;&nbsp;&nbsp; 
&nbsp;&nbsp;if(ignore &#124;&#124; request.getCharacterEncoding() == null)&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&#123;&nbsp;&nbsp; 
&nbsp;&nbsp; // 如果为空先从web.xml中得到&nbsp;&nbsp; 
&nbsp;&nbsp; String encoding = selectEncoding(request);&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; if(encoding != null)&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; &#123;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;// 设置字符集编码&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;request.setCharacterEncoding(encoding);&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; &#125;&nbsp;&nbsp; 
&nbsp;&nbsp;&#125;&nbsp;&nbsp; 
&nbsp;&nbsp;// 继续执行&nbsp;&nbsp; 
&nbsp;&nbsp;chain.doFilter(request, response);&nbsp;&nbsp;&nbsp;&nbsp;
 &#125;&nbsp;&nbsp;&nbsp;&nbsp;
 // 得到字符编码&nbsp;&nbsp; 
 private String selectEncoding(ServletRequest request)&nbsp;&nbsp;&nbsp;&nbsp;
 &#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;return encoding;&nbsp;&nbsp;&nbsp;&nbsp;
 &#125;&nbsp;&nbsp;&nbsp;&nbsp;
 public void destroy()&nbsp;&nbsp;&nbsp;&nbsp;
 &#123;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp; 
 &#125;&nbsp;&nbsp;&nbsp;&nbsp;
&#125;&nbsp;&nbsp;
</textarea><br/>如果是url的get提交参数带中文的 <br/>可以改下面这个文件，加入一个URIEncoding="GBK" <br/>X:&#92;Tomcat 5.5&#92;conf&#92;Server.xml <br/>如果是url的get提交参数带中文的 <br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">X:&#92;Tomcat 5.5&#92;conf&#92;Server.xml </div></div><br/>jsp头文件<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><%@ page contentType="text/html;charset=gbk"%></div></div><br/>//此处的charset的值要和web.xml里的&nbsp;&nbsp;<param-value></param-value>值一样。<br/>单个类的乱码可以这样来解决：tempStr = new String(str.getBytes("iso-8859-1"),"gb2312");也就是直接设置编码为gb2312。<br/>Tags - <a href="https://www.heckjj.com/tags/%25E4%25B8%25AD%25E6%2596%2587%25E4%25B9%25B1%25E7%25A0%2581/" rel="tag">中文乱码</a> , <a href="https://www.heckjj.com/tags/%25E8%25BF%2587%25E6%25BB%25A4%25E5%2599%25A8/" rel="tag">过滤器</a> , <a href="https://www.heckjj.com/tags/filter/" rel="tag">filter</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/chinese-garbled-filter-solution/#blogcomment</link>
<title><![CDATA[[评论] 解决中文乱码问题，过滤器配置，get post提交乱码，filter,struts乱码，jsp乱码]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/chinese-garbled-filter-solution/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>