• 编码过滤器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>
    



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

  • 相关阅读:
    得不到的永远在骚动
    这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧【转】
    Windows CMD命令大全【转】
    创业为什么选择北上广?
    为什么你还把时间浪费在无效人脉社交上?
    五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
    mysql分区操作
    程序员常用网站
    4种提升SQL查询性能的知识
    获得局域网内其他成员的信息
  • 原文地址:https://www.cnblogs.com/shipeng22022/p/4614121.html
Copyright © 2020-2023  润新知