struts中经常遇到提交中文乱码问题,既是头疼,所以写了一个过滤器,用来处理乱码问题。
web.xml设置对页面提交请求字符集进行设置:
1,首先在web.xml中设置对字符集过滤器
2,编写相关过滤器类
1,首先在web.xml中设置对字符集过滤器
这里设置字符集为:GBK
过滤类为: SetCharacterEncodingFilter <context-param>
2,编写过滤类
http://www.glassfanr.com/thread-176-1-1.html
来自: 谷歌眼镜社区 尊重他人劳动成果,转载请说明出处
web.xml设置对页面提交请求字符集进行设置:
1,首先在web.xml中设置对字符集过滤器
2,编写相关过滤器类
1,首先在web.xml中设置对字符集过滤器
这里设置字符集为:GBK
过滤类为: SetCharacterEncodingFilter <context-param>
<param-name>weblogic.httpd.inputCharset./*</param-name>
<param-value>GBK</param-value>
</context-param>
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>com.bjzfy.zfzj.gg.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
2,编写过滤类
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;
import javax.servlet.UnavailableException;
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 {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
try{
request.setCharacterEncoding(encoding);
}catch(Exception e){
e.printStackTrace();
}
}
// Pass control on to the next filter
try{
chain.doFilter(request, response);
}
catch(IOException e){
e.printStackTrace();
}
catch(ServletException se){
se.printStackTrace();
}
}
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);
}
}
来自: 谷歌眼镜社区 尊重他人劳动成果,转载请说明出处