package com.xja.studentexam.filter;
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;
@WebFilter(value = "/*", initParams = { @WebInitParam(name = "encoding", value = "utf-8") })
public class RequestFilter implements Filter {
private FilterConfig fConfig;
public RequestFilter() {
// 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 {
String encoding = fConfig.getInitParameter("encoding");
System.out.println("请求字符集" + encoding);
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
this.fConfig = fConfig;
}
}
注解加过滤器设置编码格式
最新推荐文章于 2021-07-28 12:01:23 发布