关闭

Struts-Filter配置详解

标签: Filter Struts javawe
135人阅读 评论(0) 收藏 举报
分类:

新建Filter,继承自servlet,代码大致如下:

package com.sw.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
 * Servlet Filter implementation class MyFilter1
 */
@WebFilter("/MyFilter1")
public class MyFilter1 implements Filter {

    /**
     * Default constructor. 
     */
    public MyFilter1() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		// pass the request along the filter chain
		request.setCharacterEncoding("utf-8");//设置接收编码
		response.setCharacterEncoding("utf-8");
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}

web.xml配置如下:

 <filter>
  	<filter-name>MyFilter1</filter-name>
  	<filter-class>com.sw.filters.MyFilter1</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
 
 <filter-mapping>
 <filter-name>MyFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:183517次
    • 积分:5996
    • 等级:
    • 排名:第4357名
    • 原创:436篇
    • 转载:62篇
    • 译文:3篇
    • 评论:49条
    博客专栏
    最新评论