创建一个过滤器(Java)

创建一个过滤器来使代码更加简洁,以消除重复的内容!!!

package tool;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;

@WebFilter(urlPatterns = { "/*" })
public class EncodingFilter implements Filter {
    public void doFilter(
        ServletRequest request,
        ServletResponse response,
        FilterChain chain
    ) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        System.out.println("フィルター処理前");

        chain.doFilter(request, response);

        System.out.println("フィルター処理後");

    }

    public void init(FilterConfig filterconfig) {}

    public void destory() {
    }
}
广告
将在 10 秒后关闭
bannerAds