struts中用过滤器(Filter)解决数据库中文的问题

news/2024/8/27 21:44:12 标签: filter, struts, 数据库, encoding, character, null

 struts中用过滤器(Filter)解决数据库中文的问题
在web.xml中加上
  <filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.huahang.tj.struts.filters.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>
这里会涉及一个Filter,源码如下:
import javax.servlet.*;
import java.io.IOException;

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

    }

加上这个后,在action中就可以直接从form中接收gb2312编码的数据了,返回时自然也是gb2312了


http://www.niftyadmin.cn/n/1425535.html

相关文章

Shell截取字符串方法总结

Linux 的字符串截取很有用。有八种方法。 假设有变量 varhttp://www.aaa.com/123.htm. 1. # 号截取&#xff0c;删除左边字符&#xff0c;保留右边字符。 echo ${var#*//} 其中 var 是变量名&#xff0c;# 号是运算符&#xff0c;*// 表示从左边开始删除第一个 // 号及左边…

Struts HTML标签库学习笔记

Struts HTML标签库学习笔记 <html:form> <html:form>用来创建表单,<html:form>必须包含一个action属性,否则JSP会抛出一个异常. 常用属性: Action 指定用户提交的表单由哪个组件来处理 Enctype 指定表单所用的MIME…

shell 字符串替换

read in_cmd echo $in_cmd > a sed s/[[:space:]]//g -i a ##replace space by ##去掉所以的空格 sed s/,//g -i a ##replace , by ##去掉所以的逗号 va$(cat a)接下就可以对变量va进行判断了。 如果只是输出打印的话&#xff0c;直接输出就可以 re…

突然使用不了JSTL了

在我的JSP页面中我使用了JSTL代码如下&#xff1a; ${user.username} 其中user是存在session中的变量&#xff0c;已经测试过是不为null的&#xff0c;JSTL的标签库、jar相关的文件已导入。 但是在浏览器中却是把“${user.username}”这句话原封不动地显示出来。根本读取不了${…

drivers/staging

staging tree建立之目的是用来放置一些未充分测试或者因为一些其他原因未能进入内核的新增驱动程序和新增文件系统。

将博客搬至CSDN_wuli大世界_新浪博客

各位粉条儿们&#xff0c;大家好&#xff1a;之前没有发现&#xff0c;竟然还有CSDN这种专门提供给IT界人士的更具专业性的博客。当知道之后&#xff0c;真的有点后悔&#xff0c;为什么当初把IT技术文档发布在了新浪呢&#xff1f;&#xff08;当然&#xff0c;这并没有恶意&a…

servlet传值怎么取不到呀,要用第二种跳转方法

servlet中跳转的有两种方法&#xff1a;方法一&#xff1a;response.sendRedirect("../success.jsp")利用这个方法如果跳转前有request.setAttribute("a","b")的话&#xff0c;那么参数是传不到success.jsp&#xff0c;因为它在客户端重定向到su…

以太网最小字节长度和最大字节长度

最小为64,最大为1518&#xff08;mtu为1500&#xff09;&#xff0c;但是有了巨型帧最大为9K&#xff0c;发送时分成一片一片的MTU发送 具体详解&#xff1a;http://blog.sina.com.cn/s/blog_413d250e01017i72.html