读书人

设置Tomcat及Weblogic字符编码解决数

发布时间: 2012-08-22 09:50:35 作者: rapoo

设置Tomcat及Weblogic字符编码,解决数据提交的乱码问题

本文原文来自:http://www.cublog.cn/u/32768/showart_360298.html
设置Tomcat及Weblogic字符编码,解决数据提交的乱码问题
【关键字】 Tomcat容器 中文 字符编码 乱码 Weblogic容器 服务器

【正文】因为编码方式的不同在使用Tomcat容器时会出现提交到Servlet的中文是乱码的方式,而且Tomcat5.x对于POST和GET的方式处理似乎还有不同,POST方式是采用Filter的方式即可,怎样能够处理GET方式中文提交乱码的问题呢?

可以采用配置服务器字符编码的方法,具体操作如下:
1、打开Tomcat安装目录中的conf目录
2、修改server.xml中的connector一个子项,具体可能类似如下:


在其中添加URIEncoding="GBK" ,或者是其他的编码方式,变成如下:


3、停止Tomcat服务,重新启动Tomcat服务即可。

【另附Weblogic容器处理编码的方式】

处理Weblogic容器的编码比较简单,只需要在站点的web.xml中配置一行如下的代码即可。

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);    }}

读书人网 >Web前端

热点推荐