java 汉字乱码问题解决方法
下面关于网页特效phttp://www.111cn.net/网页特效p.html target=_blank >jsp教程汉字乱码问题我们是做了一个过滤器,然后到web中配置一下,这样很好的解决了jsp中汉字编码问题哦。
*/
public class characterfilter implements filter {
public void destroy() {
}
public void dofilter(servletrequest request, servletresponse response,
filterchain filter) throws ioexception, servletexception {request.setcharacterencoding("utf-8");
response.setcharacterencoding("utf-8");
filter.dofilter(request, response);
}public void init(filterconfig arg0) throws servletexception {
}
}
%>
再到 web配置中www.111cn.net
<filter>
<filter-name>characterfilter</filter-name>
<filter-class>org.shopping.uitl.characterfilter</filter-class>
</filter>再来看一下官方的对汉字乱码处理方法吧。mb.111cn.net
<%
import java.io.ioexception;
import javax.servlet.filter;
import javax.servlet.filterchain;
import javax.servlet.filterconfig;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;
/**
* 过滤器,设置文字编码格式
*/
public class setcharacterencodingfilter implements filter {protected string encoding = null;
protected filterconfig filterconfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterconfig = null;}
public void dofilter(servletrequest request, servletresponse response,
filterchain chain)
throws ioexception, servletexception {if (ignore || (request.getcharacterencoding() == null)) {
string encoding = selectencoding(request);
if (encoding != null)
request.setcharacterencoding(encoding);
}chain.dofilter(request, response);
}
public void init(filterconfig filterconfig) throws servletexception {
this.filterconfig = filterconfig;
this.encoding = filterconfig.getinitparameter("encoding");
string value = filterconfig.getinitparameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsignorecase("true"))
this.ignore = true;
else if (value.equalsignorecase("yes"))
this.ignore = true;
else
this.ignore = false;}
protected string selectencoding(servletrequest request) {
return (this.encoding);
}
}
%>
过滤器在xml中的配置:
xml code
<filter>
<filter-name>setcharacterencodingfilter</filter-name>
<filter-class>
包名.setcharacterencodingfilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setcharacterencodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>