エンコードフィルタFilter

2832 ワード

エンコードフィルタの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>