類似下面的過濾方式
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.usl</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.view</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/jaxrs/*</url-pattern>
</filter-mapping>
在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結尾的可以過。