`
侯风玄黄
  • 浏览: 42356 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

使用Filter转换字符集

阅读更多

1,加入类 com.ddy.struts.filter.SetCharacterEncodingFilter

package com.ddy.struts.filter; 
import javax.servlet.*;
import java.io.IOException; 
/**
 * <p>
 * Filter that sets the character encoding to be used in parsing the incoming
 * request, either unconditionally or only if the client did not specify a
 * character encoding. Configuration of this filter is based on the following
 * initialization parameters:
 * </p>
 * <ul>
 * <li><strong>encoding</strong> - The character encoding to be configured for
 * this request, either conditionally or unconditionally based on the
 * <code>ignore</code> initialization parameter. This parameter is required,
 * so there is no default.</li>
 * <li><strong>ignore</strong> - If set to "true", any character encoding
 * specified by the client is ignored, and the value returned by the
 * <code>selectEncoding()</code> method is set. If set to "false,
 * <code>selectEncoding()</code> is called <strong>only</strong> if the
 * client has not already specified an encoding. By default, this parameter is
 * set to "true".</li>
 * </ul>
 *
 * <p>
 * Although this filter can be used unchanged, it is also easy to subclass it
 * and make the <code>selectEncoding()</code> method more intelligent about
 * what encoding to choose, based on characteristics of the incoming request
 * (such as the values of the <code>Accept-Language</code> and
 * <code>User-Agent</code> headers, or a value stashed in the current user's
 * session.
 * </p>
 *
 * @author <a href="mailto:jwtronics@yahoo.com">John Wong</a>
 *
 * @version $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27
 *          johnwong Exp $
 */
public class SetCharacterEncodingFilter implements Filter { 
 // ----------------------------------------------------- Instance Variables 
 /**
  * The default character encoding to set for requests that pass through this
  * filter.
  */
 protected String encoding = null; 
 /**
  * The filter configuration object we are associated with. If this value is
  * null, this filter instance is not currently configured.
  */
 protected FilterConfig filterConfig = null; 
 /**
  * Should a character encoding specified by the client be ignored?
  */
 protected boolean ignore = true; 
 // --------------------------------------------------------- Public Methods 
 /**
  * Take this filter out of service.
  */
 public void destroy() { 
  this.encoding = null;
  this.filterConfig = null; 
 } 
 /**
  * Select and set (if specified) the character encoding to be used to
  * interpret request parameters for this request.
  *
  * @param request
  *            The servlet request we are processing
  * @param result
  *            The servlet response we are creating
  * @param chain
  *            The filter chain we are processing
  *
  * @exception IOException
  *                if an input/output error occurs
  * @exception ServletException
  *                if a servlet error occurs
  */
 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)
    request.setCharacterEncoding(encoding);
  } 
  // Pass control on to the next filter
  chain.doFilter(request, response); 
 } 
 /**
  * Place this filter into service.
  *
  * @param filterConfig
  *            The filter configuration object
  */
 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 Methods 
 /**
  * Select an appropriate character encoding to be used, based on the
  * characteristics of the current request and/or filter initialization
  * parameters. If no character encoding should be set, return
  * <code>null</code>.
  * <p>
  * The default implementation unconditionally returns the value configured
  * by the <strong>encoding</strong> initialization parameter for this
  * filter.
  *
  * @param request
  *            The servlet request we are processing
  */
 protected String selectEncoding(ServletRequest request) { 
  return (this.encoding); 
 } 
}// EOC

 

2,修改WEB.XML,配置filter

   <filter>
   <filter-name>Set Character Encoding</filter-name>
   <filter-class>
    com.ddy.struts.filter.SetCharacterEncodingFilter
   </filter-class>
   <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>
   </init-param>
   <init-param>
    <param-name>ignore</param-name>
    <param-value>true</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>Set Character Encoding</filter-name>
   <servlet-name>action</servlet-name>
  </filter-mapping> 

 

3,设定jsp页面的编码(必须于上面encoding标签中设定一致)

     <%@ page language="java" contentType="text/html; charset=GB2312"
              pageEncoding="GB2312"%>

 

分享到:
评论

相关推荐

    bloomf:JavaScript中的Bloom Filter实现

    可以通过将不同的元素类型(数字,字符串)转换为字符串来插入它们。用法const BloomFilter = require ( 'bloomf' ) ;const filterSize = 10 ;const kHashes = 3 ;const bl = new BloomFilter ( filterSize , ...

    论文研究 - 预处理用于OCR转换的公共标牌图像

    然后使用阈值技术对图像进行二值化,该技术针对在对比背景上打印的文本进行了优化,并通过Tesseract引擎检测单个字符。 我们在大学校园内外拍摄的200多幅图像的数据集上测试了我们的技术,并且与传统方法相比,成功...

    一种面向深度包检测的DFA压缩算法

    DFA(确定性有限自动机)对于实现深度包检测(deep packet inspection,DPI)技术具有重要作用。随着深度包检测规则的不断...实验结果表明,该算法在L7-filter和Snort规则集上具有较稳定的压缩率,压缩率都在5%以下。

    基于opencv3.1库的JAVA源码

    范例5-4-4模糊处理-使用BoxFilter函数及GUI元件 119 范例5-5-1 Threshold-使用临界值函数及GUI元件 122 范例5-5-2 AdaptiveThreshold-使用自适临界值及GUI元件 128 范例5-6-1 Sharpness锐利化处理 130 范例5-6-2锐利...

    Excel公式与函数大辞典.宋翔(带书签高清文字版).pdf

    5.4.2 WIDECHAR——将半角字符转换为全角字符 175 5.4.3 PHONETIC——返回文本中的拼音字符 176 5.4.4 BAHTTEXT——将数字转换为泰语文本 176 5.4.5 DOLLAR——将数字转换为带美元符号$的文本 176 5.4.6 RMB...

    最新AngularJS开发宝典视频教程 后盾网AngularJS培训视频教程 后盾网.txt

    ├最新AngularJS开发宝典—第036讲 使用limitTo过滤器截取字符.mp4 ├最新AngularJS开发宝典—第037讲 使用date过滤器处理时间.mp4 ├最新AngularJS开发宝典—第038讲 orderBy数据排序与filter筛选过滤器.mp4 ├最新...

    informatica文档

    4.在Source Qualifier中的多个地方可以使用参数或变量,传入SQL语句用的是字符串格式,所以大部分时候都需要用引号; 5.Source Qualifier只为连出的Port产生SQL语句,需要注意的是,Override SQL是和连出线的Port...

    2019千峰Python超详细入门教程(百度云盘分享).docx

    ├─千锋Python教程:第03章 字符串&布尔&空值(7集) │ │ .DS_Store │ │ │ ├─code │ │ 1、String(字符串).py │ │ 2、String的内置函数.py │ │ 3、布尔值和空值.py │ │ 4、变量的类型问题.py │ │ ...

    asp.net知识库

    Tool Tip 示例(FILTER版) Tool Tip示例 (htc版) 一个.net发送HTTP数据实体的类 按键跳转以及按Enter以不同参数提交,及其他感应事件 动态控制Page页的Head信息 SubmitOncePage:解决刷新页面造成的数据重复提交...

    asp 完全教程

    Split(Temp, ", ") %&gt; 此处的JScript join()方法将数组myJSArray元素转换到一个以逗号为分割符的字符串, VBScript Split()函数将字符串转换为VBScript数组。注意这里我们是在VBScript环境下调用JScript的...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part2

    21.1.1 常用字符集 610 21.1.2 对乱码产生过程的分析 612 21.2 中文乱码问题的解决方案 614 21.3 使用过滤器解决中文问题 616 21.4 让tomcat支持中文文件名 620 21.5 国际化与本地化 621 21.5.1 locale 621.. ...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part5

    21.1.1 常用字符集 610 21.1.2 对乱码产生过程的分析 612 21.2 中文乱码问题的解决方案 614 21.3 使用过滤器解决中文问题 616 21.4 让tomcat支持中文文件名 620 21.5 国际化与本地化 621 21.5.1 locale 621.. ...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part4

    21.1.1 常用字符集 610 21.1.2 对乱码产生过程的分析 612 21.2 中文乱码问题的解决方案 614 21.3 使用过滤器解决中文问题 616 21.4 让tomcat支持中文文件名 620 21.5 国际化与本地化 621 21.5.1 locale 621.. ...

    JAVA WEB 开发详解:XML+XSLT+SERVLET+JSP 深入剖析与实例应用.part3

    21.1.1 常用字符集 610 21.1.2 对乱码产生过程的分析 612 21.2 中文乱码问题的解决方案 614 21.3 使用过滤器解决中文问题 616 21.4 让tomcat支持中文文件名 620 21.5 国际化与本地化 621 21.5.1 locale 621.. ...

    one-country-page

    数据中所有国家/地区均使用大写3字母iso_code 条形图色彩模式:行调色板:单色网址设置: color.categorical_custom_palette 折线图系列过滤器:多选显示的最大系列:〜3 网址设置: series_filter数据有3个数据集...

    精通windows server 2008 命令行与powershell 电子书PDF单文件完整版

    3.4.4 graftabl——启用扩展字符集 119 3.4.5 mode——系统设置 121 3.4.6 path——路径 125 3.4.7 reg——修改注册表子项 125 3.4.8 regedit——注册表编辑器 132 3.4.9 regsvr32——将dll文件注册为命令 132 ...

    C#全能速查宝典

    1.4.45 ToShortDateString方法——转换为短日期字符串 88 1.4.46 ToShortTimeString方法——转换为短时间字符串 88 1.4.47 ToString方法——转换为字符串 89 1.4.48 ToUpper方法——转换为大写 90 1.4.49 Trim方法...

    ssh(structs,spring,hibernate)框架中的上传下载

    这是Hibernate3引入的新特性,对于包含重量级大数据的表字段,这种抽取方式提高了对大字段操作的灵活性,否则加载Tfile对象的结果集时如果总是返回fileContent,这种批量的数据抽取将可以引起数据库的"洪泛效应"。...

    ZendFramework中文文档

    1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) ...14.5.5. Adding Filter ...

    Delphi5开发人员指南

    2.8 强制类型转换和类型约定 46 2.9 字符串资源 47 2.10 测试条件 47 2.10.1 if语句 47 2.10.2 case语句 48 2.11 循环 49 2.11.1 for循环 49 2.11.2 while循环 49 2.11.3 repeat...until 50 2.11.4 Break()过程 50 ...

Global site tag (gtag.js) - Google Analytics