• 编码过滤器Filter


    编码过滤器的java文件:

    CharsetFilter.java:

    package com.house.util;
    /**
     * @author
     * @version 
     * 创建时间:2006-12-13 15:08:35
     * 类说明:
     */
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    
    import java.io.IOException;
    
    
    
    
    public class CharsetFilter implements Filter {
    
    
    	
    	private String targetencoding = "UTF-8";
    	
    	protected FilterConfig filterConfig = null;
    
    
    	public void destroy() {
    		
    		this.filterConfig = null;
    	}
    	
    	public void doFilter(ServletRequest req, ServletResponse resp,
    			FilterChain chain) throws IOException, ServletException {
    
    
    		HttpServletRequest request = (HttpServletRequest)req;
    		request.setCharacterEncoding(targetencoding);
    		chain.doFilter(req,resp);
    	}
    
    
    	public void init(FilterConfig filterConfig) throws ServletException {
    		this.filterConfig = filterConfig;
    		this.targetencoding = filterConfig.getInitParameter("encoding");
    
    
    	}	
    }
    


    web.xml文件  配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name />
      <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
          <param-name>debug</param-name>
          <param-value>3</param-value>
        </init-param>
        <init-param>
          <param-name>detail</param-name>
          <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
      <filter>
        <filter-name>encoding</filter-name>
        <filter-class>com.house.util.CharsetFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    [java tool]sonar与idea结合使用,度量代码质量
    【mockito】单元测试之mockito简单使用
    oracle ,mysql,postgres jdbc配置文件
    log4j配置文件及java调用 每个级别输出到不同的文件2
    log4j配置文件及java调用 每个级别输出到不同的文件
    tomcat编码问题
    pf4j实例 插件框架
    网络协议分为哪几层---物理层,连接层,网络层,传输层,应用层详解
    TF-IDF及其算法
    redhat7查看系统版本 修改主机名
  • 原文地址:https://www.cnblogs.com/shipeng22022/p/4614176.html
Copyright © 2020-2023  润新知