使用过滤器对所有 action 进行自动过滤。代码参考如下,别忘在 web.xml 中配置这个过滤器,初始化参数为 GBK.package com.alvin;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 CharacterEncodingFilter implements Filter
{ protected String encoding = null; public void init(FilterConfig filterConfig) throws ServletException
{
this.encoding = filterConfig.getInitParameter("encoding");
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
request.setCharacterEncoding(encoding);
//response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
} public void destroy() {
// TODO 自动生成方法存根
}}
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class CharacterEncodingFilter implements Filter
{ protected String encoding = null; public void init(FilterConfig filterConfig) throws ServletException
{
this.encoding = filterConfig.getInitParameter("encoding");
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
request.setCharacterEncoding(encoding);
//response.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
} public void destroy() {
// TODO 自动生成方法存根
}}
解决方案 »
- 在windows机器上的文件显示正常而转移到Linuix机器上就会显示乱码,在线求帮助
- 求j2ee项目文档,源码,视频(40分)
- 关于Spring的xslt视图显示问题
- velocity 中 双引号怎么转义?
- wsdl文件拿来生成java类,在通过类来发布服务!!高手进进
- https 跟 http 转换
- 50分求精通struts基於mvc的java web設計與開發的光盤,
- 大家都用什么IDE来开发web service 如果用,eclipse有什么插件可用吗 ?
- j2ee配置出错,请大侠帮帮忙
- 网络难题!
- 急聘高级java开发工程师、技术主管
- 大家遇到过这样的问题吗:source 1.4 中不支持泛型
ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);方便。呵呵
其他处理参考一楼。