jsp代码:
import javax.servlet.*;
import javax.servlet.http.*;
public class charsetfilter implements filter
{
public void destroy()
{
}
public void dofilter(servletrequest request, servletresponse response, filterchain chain)
{
try
{
httpservletrequest httprequest = (httpservletrequest)request;
string method = httprequest.getmethod().tolowercase();
if(method.equals("post"))
{
//如果是post,即表单方法,直接设置charset即可
request.setcharacterencoding("utf-8");
}
else if(method.equals("get"))
{
//如果是get方法
request.setcharacterencoding("utf-8");
request = new httpservletrequestwrapper((httpservletrequest)request)
{
public string getparameter(string str)
{
try
{
return new string(super.getparameter(str).getbytes("iso-8859-1"),"gbk");
}
catch(exception e)
{
return null;
}
}
};
}
chain.dofilter(request, response);
}
catch(exception e){}
}
public void init(filterconfig filterconfig)
{
}
}
过滤器配置:
charfilter
charsetfilter
charfilter
/*
request
希望与广大网友互动??
点此进行留言吧!