国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
攔截器和過濾器的區(qū)別(轉(zhuǎn))
1、攔截器是基于java的反射機制的,而過濾器是基于函數(shù)回調(diào)
2、過濾器依賴與servlet容器,而攔截器不依賴與servlet容器
3、攔截器只能對action請求起作用,而過濾器則可以對幾乎所有的請求

起作用
4、攔截器可以訪問action上下文、值棧里的對象,而過濾器不能
5、在action的生命周期中,攔截器可以多次被調(diào)用,而過濾器只能在容

器初始化時被調(diào)用一次

攔截器 :是在面向切面編程的就是在你的service或者一個方法,前調(diào)用一個方法,或者在方法后調(diào)用一個方法比如動態(tài)代理就是攔截器的簡單實現(xiàn),在你調(diào)用方法前打印出字符串(或者做其它業(yè)務(wù)邏輯的操作),也可以在你調(diào)用方法后打印出字符串,甚至在你拋出異常的時候做業(yè)務(wù)邏輯的操作。

下面通過實例來看一下過濾器和攔截器的區(qū)別:
使用攔截器進行/admin 目錄下jsp頁面的過濾
<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>
下面是我實現(xiàn)的Interceptor class:
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
     }
}
       過濾器:是在java web中,你傳入的request,response提前過濾掉一些信息,或者提前設(shè)置一些參數(shù),然后再傳入servlet或者struts的 action進行業(yè)務(wù)邏輯,比如過濾掉非法url(不是login.do的地址請求,如果用戶沒有登陸都過濾掉),或者在傳入servlet或者 struts的action前統(tǒng)一設(shè)置字符集,或者去除掉一些非法字符.
使用過濾器進行/admin 目錄下jsp頁面的過濾,首先在web.xml進行過濾器配置:
    <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>
下面是過濾的實現(xiàn)類:
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 {
     }
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
過濾器與攔截器的區(qū)別
過濾器和攔截器的比較及未登錄用戶權(quán)限限制的實現(xiàn)
spring中過濾器(filter)、攔截器(interceptor)和切面(aop)的執(zhí)行順序
攔截器(Interceptor)和過濾器(Filter)的執(zhí)行順序和區(qū)別
通過struts2攔截器實現(xiàn)權(quán)限管理
servlet/filter/listener/interceptor區(qū)別與聯(lián)系
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服