【JSP】EnCodingFilter

package com.firstJSP.util;


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;
import javax.servlet.annotation.WebInitParam;

// 注解配置Filter
@WebFilter(filterName="EnCodingFilter",urlPatterns="/*",initParams={@WebInitParam(name="encoding",value="UTF-8")})
public class EnCodingFilter implements Filter {


private String encoding;
public EnCodingFilter() {
    }


public void destroy() {
}


/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}


public void init(FilterConfig fConfig) throws ServletException {
encoding = fConfig.getInitParameter("encoding");
}


}


web.xml配置Filter


enCodingFilter
com.firstJSP.filter.EnCodingFilter

encoding
UTF-8


enCodingFilter
/*


Tomcat配置

port="8080" protocol="HTTP/1.1" redirectPort="8443" />

你可能感兴趣的:(JSP)