Ⅰ java過濾器中的FilterChain對象
1、什麼是過濾器?
與Servlet相似,過濾器是一些web應用程序組件,可以綁定到一個web應用程序中。但是與其他web應用程序組件不同的是,過濾器是"鏈"在容器的處理過程中的。這就意味著它們會在servlet處理器之前訪問一個進入的請求,並且在外發響應信息返回到客戶前訪問這些響應信息。這種訪問使得過濾器可以檢查並修改請求和響應的內容。
2、過濾鏈FilterChain
兩個過濾器,EncodingFilter負責設置編碼,SecurityFilter負責控制許可權,伺服器會按照web.xml中過濾器定義的先後循序組裝成一條鏈,然後一次執行其中的doFilter()方法。執行的順序就如下圖所示,執行第一個過濾器的chain.doFilter()之前的代碼,第二個過濾器的chain.doFilter()之前的代碼,請求的資源,第二個過濾器的chain.doFilter()之後的代碼,第一個過濾器的chain.doFilter()之後的代碼,最後返回響應。
3、過濾鏈的好處是,執行過程中任何時候都可以打斷,只要不執行chain.doFilter()就不會再執行後面的過濾器和請求的內容。而在實際使用時,就要特別注意過濾鏈的執行順序問題,像EncodingFilter就一定要放在所有Filter之前,這樣才能確保在使用請求中的數據前設置正確的編碼。
Ⅱ jsp/servlet過濾器和struts2攔截器的有什麼區別
攔截器和過濾器的區別:
1、攔截器是基於java的反射機制的,而過濾器是基於函數回調
2、過濾器依賴與servlet容器,而攔截器不依賴與servlet容器
3、攔截器只能對action請求起作用,而過濾器則可以對幾乎所有的請求起作用
4、攔截器可以訪問action上下文、值棧里的對象,而過濾器不能
5、在action的生命周期中,攔截器可以多次被調用,而過濾器只能在容器初始化時被調用一次
攔截器
:是在面向切面編程的就是在你的service或者一個方法前調用一個方法,或者在方法後調用一個方法比如動態代理就是攔截器的簡單實現,在你調用方法前列印出字元串(或者做其它業務邏輯的操作),也可以在你調用方法後列印出字元串,甚至在你拋出異常的時候做業務邏輯的操作。
下面通過實例來看一下過濾器和攔截器的區別:
使用攔截器進行/admin 目錄下jsp頁面的過濾
[html] view plain
<package name="newsDemo"
extends="struts-default"
namespace="/admin">
<interceptors>
<interceptor name="auth"
class="com.test.news.util.AccessInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref
name="auth" />
</interceptor-stack>
</interceptors>
<!-- action -->
<action name="newsAdminView!*" class="newsAction"
method="{1}">
<interceptor-ref
name="defaultStack"/>
<interceptor-ref
name="authStack">
</interceptor-ref>
下面是我實現的Interceptor class:
[java] view plain
package com.test.news.util;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import
com.opensymphony.xwork2.ActionInvocation;
import
com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import
com.test.news.action.AdminLoginAction;
/**
*
@author chaoyin
*/
public class AccessInterceptor
extends AbstractInterceptor {
private static final long
serialVersionUID = -4291195782860785705L;
@Override
public String intercept(ActionInvocation actionInvocation) throws
Exception {
ActionContext actionContext =
actionInvocation.getInvocationContext();
Map session =
actionContext.getSession();
//except login action
Object action = actionInvocation.getAction();
if (action
instanceof AdminLoginAction) {
return
actionInvocation.invoke();
}
//check
session
if(session.get("user")==null ){
return
"logout";
}
return actionInvocation.invoke();//go
on
}
}
過濾器:是在javaweb中,你傳入的request,response提前過濾掉一些信息,或者提前設置一些參數,然後再傳入servlet或者struts的
action進行業務邏輯,比如過濾掉非法url(不是login.do的地址請求,如果用戶沒有登陸都過濾掉),或者在傳入servlet或者
struts的action前統一設置字元集,或者去除掉一些非法字元。
使用過濾器進行/admin
目錄下jsp頁面的過濾,首先在web.xml進行過濾器配置:
[html] view plain
<filter>
<filter-name>access
filter</filter-name>
<filter-class>
com.test.news.util.AccessFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>access filter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
下面是過濾的實現類:
[java] view
plain
package com.test.news.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.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.HttpSession;
public class AccessFilter
implements Filter {
/**
* @author chaoyin
*/
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
if(session.getAttribute("user")== null &&
request.getRequestURI()。indexOf("login.jsp")==-1 ){
response.sendRedirect("login.jsp");
return ;
}
filterChain.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
摘自網路--
Ⅲ java中過濾器(Filter)與攔截器(Interceptor)的區別
Java中過濾器與攔截器的主要區別如下:
執行位置:
實現方式:
作用范圍:
執行流程:
綜上所述,過濾器主要用於控制請求流向和進行全局性的預處理和後處理,而攔截器則主要用於執行通用的、跨切面的代碼邏輯,並專注於方法級別的攔截。理解兩者之間的區別有助於在Java Web開發中選擇合適的工具來實現特定的功能。