读书人

解决跨应用服务器WEBLOGIC、TOMCAT编码

发布时间: 2012-09-05 15:19:34 作者: rapoo

解决跨应用服务器WEBLOGIC、TOMCAT编码问题

一、  描述项目编码一向做为系统设计级别难题,servlet2.4还未实现跨应用服务器编码支持,个应用服务器之间编码存在很大差异,因此导致部署不同应用服务器上会出现不同乱码问题。为了实现开发环境(tomcat)和生产环境(weblogic9.2)编码统一,特此实现WEB.XML配置FILTER过滤编码,经过多次测试,基本实现跨应用服务器编码统一。二、  配置(工程编码:UTF-8)1.  个人编码规范由于一些模块存在GET请求,在处理request.getParameter后出现乱码问题,一般个人实现方式为dwmc = new String(dwmc.getBytes("iso-8859-1"), "gb2312"); 此转码形式只适合一种应用服务器tomcat,为保证生产环境下weblogic能正常编码,需要:完全清理因个人书写而改变编码设置代码。并使用系统编码设置。2.  系统配置TOMCATPOST:系统web.xml中使用filter进行过滤POST请求,并其自动对其编码转换为utf-8。实现代码为 request.setCharacterEncoding(encod);GET:filter过滤无法为TOMCAT进行GET编码转换,因此需要修改TOMCAT/CONF/ server.xml文件,在项目Connector标签下增加URIEncoding="GB2312"属性。3.  系统配置WEBLOGIC9.2POST与GET统一处理方式,使用FILTER进行处理。判断request.getContentType()空的时候则为GET 请求,处理如下:request.setCharacterEncoding(encod_wlc);response.setContentType("application/x-www-form-urlencoded;charset="+ encod_wlc);response.setCharacterEncoding(encod_wlc);encod_wlc:为配置参数,此处:GB2312POST请求则正常转换成utf-8即可,request.setCharacterEncoding(encod);三、  web.xml配置    <filter>        <filter-name>encodingFilter</filter-name>        <filter-class>com.vstsoft.csi.util.SetCharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>//默认POST编码规则         </init-param>        <init-param>            <param-name>encoding_wlc</param-name>            <param-value>GB2312</param-value>//默认WEBLOGIC下GET编码规则         </init-param>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>*.do</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>*.jsp</url-pattern></filter-mapping> 四、  JAVA文件package com.vstsoft.csi.util;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 String encoding_wlc = null;    protected FilterConfig filterConfig = null;    protected boolean ignore = true;    public void destroy() {        this.encoding = null;        this.encoding_wlc=null;        this.filterConfig = null;    }    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {        if (ignore || (request.getCharacterEncoding() == null)) {            String encod = selectEncoding(request);            String encod_wlc = selectEncoding_wlc(request);            if (request.getContentType() == null) {                request.setCharacterEncoding(encod_wlc);                response.setContentType("application/x-www-form-urlencoded; charset="+ encod_wlc);                response.setCharacterEncoding(encod_wlc);            } else {                request.setCharacterEncoding(encod);            }            encod=null;            encod_wlc=null;        }        chain.doFilter(request, response);    }    public void init(FilterConfig filterConfig) throws ServletException {        this.filterConfig = filterConfig;        this.encoding = filterConfig.getInitParameter("encoding");        this.encoding_wlc = filterConfig.getInitParameter("encoding_wlc");        String value = filterConfig.getInitParameter("ignore");        System.out.println("======>编码过滤初始化, 默认POST请求编码: "+this.encoding+", WBL下GET请求编码: "+this.encoding_wlc+"<=====");        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);    }        protected String selectEncoding_wlc(ServletRequest request) {        return (this.encoding_wlc);    }}

?

读书人网 >Web前端

热点推荐