web.xml lt;filter-mapping>我不想过滤html文件,怎么设置

2024-11-27 12:29:36
推荐回答(2个)
回答1:

不使用 /* 拦截所有,拦截想拦截的就可以了

类似下面的过滤方式

encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8



encodingFilter
*.action


encodingFilter
*.usl


encodingFilter
*.jsp


encodingFilter
*.view


encodingFilter
/jaxrs/*

在filter类中判断一下,如果是.html结尾的就不过滤。
HttpServletRequest req = (HttpServletRequest)request;
//由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url
String reqURI = req.getRequestURI();
if(reqURI.endsWith(".html")) chain.doFilter(request, response);
HttpServletRequest req = (HttpServletRequest)request;
//由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url
String reqURI = req.getRequestURI();
if(reqURI.endsWith(".html")) chain.doFilter(request, response);

chain是以下过滤方法中的chain吗?
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException{

另外, if(reqURI.endsWith(".html")) 到底是 真还是假

HttpServletRequest req = (HttpServletRequest)request;
//由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url
String reqURI = req.getRequestURI();
if(reqURI.endsWith(".html")) chain.doFilter(request, response);

chain是以下过滤方法中的chain吗?
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException{

另外, if(reqURI.endsWith(".html")) 到底是 真还是假

就是这个chain,这个判断就是判断你放问的路径是否以.html结尾,你可以试试。

HttpServletRequest req = (HttpServletRequest)request;
//由于web.xml中设置Filter过滤全部请求,可以排除不需要过滤的url
String reqURI = req.getRequestURI();
if(reqURI.endsWith(".html")) chain.doFilter(request, response);

chain是以下过滤方法中的chain吗?
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException{

另外, if(reqURI.endsWith(".html")) 到底是 真还是假

就是这个chain,这个判断就是判断你放问的路径是否以.html结尾,你可以试试。

报错
java.lang.IllegalStateException: getWriter() has already been called for this response
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//把ServletRequest和ServletResponse转换成真正的类型
HttpServletRequest req = (HttpServletRequest)request;
String reqURI = req.getRequestURI();
if(reqURI.endsWith(".do")) chain.doFilter(request, response);
System.out.println(reqURI);
chain.doFilter(request, response);
}

我这里用过的原代码,没错,你看下你别的地方,用到response的地方
//把ServletRequest转换成真正的类型
HttpServletRequest req = (HttpServletRequest)request;
String reqURI = req.getRequestURI();
if(reqURI.endsWith(".html")) chain.doFilter(request, response);
else{
return;
}

这个是我刚才测试了的,只有html结尾的可以过。

回答2:

你可以设置想过滤的动作,这样就不过滤html了。比如下面这样:

encodingFilter
*.action


encodingFilter
*.usl


encodingFilter
*.jsp