具体代码如下所示:
//注解表示拦截所有的请求
@WebFilter("/*")
public class firstfilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//设置请求的编码格式
servletRequest.setCharacterEncoding("utf-8");
//设置响应的格式及编码方式
servletResponse.setContentType("text/html;charset=utf-8");
//放行请求
filterChain.doFilter(servletRequest,servletResponse);
}
}