一聚教程网:一个值得你收藏的教程网站

热门教程

java 汉字乱码问题解决方法

时间:2022-11-14 23:25:44 编辑:袖梨 来源:一聚教程网

java 汉字乱码问题解决方法
下面关于网页特效p/网页特效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.111com.net


characterfilter
org.shopping.uitl.characterfilter


再来看一下官方的对汉字乱码处理方法吧。mb.111com.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

setcharacterencodingfilter

包名.setcharacterencodingfilter


encoding
utf-8



setcharacterencodingfilter
/*

热门栏目