使用filter解决中文乱码问题

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

关键字: 使用filter解决中文乱码问题

一.在web.xml中配置

xml 代码

EncodingFilter

com.kenshin.base.SysFilter

encoding

UTF-8

enable

true

EncodingFilter

*.do

二.filter类代码

java 代码

package com.kenshin.base;

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 mons.logging.Log;

import mons.logging.LogFactory;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

public class SysFilter implements Filter {

protected String sEncodingName;

protected FilterConfig sysFilter;

protected boolean bEnable;

protected Log logger = LogFactory.getLog(SysFilter.class);

public void destroy() {

}

public void doFilter(ServletRequest arg0, ServletResponse arg1

FilterChain arg2) throws IOException, ServletException{

// TODO Auto-generated method stub

if (this.bEnable) {

try {

arg0.setCharacterEncoding(this.sEncodingName);

arg1.setContentType("text/html;charset=" + this.sEcodingName) ;

arg1.setCharacterEncoding(this.sEncodingName);

arg2.doFilter(arg0, arg1);

} catch (Exception e) {

("出错了");

}

// session.close();

} else {

arg2.doFilter(arg0, arg1);

}

}

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

this.sysFilter = arg0;

this.loadFilterSetting();

}

private void loadFilterSetting() {

this.sEncodingName = this.sysFilter.getInitParameter("encoding"); ("encoding:" + sEncodingName);

String sEnable = this.sysFilter.getInitParameter("enable");

if (sEnable != null && sEnable.equalsIgnoreCase("true")) {

this.bEnable = true;

} else {

this.bEnable = false; }

}

}

相关文档
最新文档