过滤器 、拦截器、监听器

过滤器:--------------------------------------------------------------------------------------------
只要你在web.xml文件配置好要拦截的客户端请求,它都会帮你拦截到请求,此时你就可以对请求或响应(Request、Response)统一设置编码,简化操作;同时还可进行逻辑判断,如用户是否已经登陆、有没有权限访问该页面等等工作。它是随你的web应用启动而启动的,只初始化一次,以后就可以拦截相关请求,只有当你的web应用停止或重新部署的时候才销毁


<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.azcsoft.servlet.MyFilter</filter-class>
<init-param>
<param-name>name</param-name>
<param-value>zzd</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

过滤器 属于Servlet 规范:只要想,可以拦截任何url, 但是不能拦截指定方法

public class MyFilter implements Filter {
FilterConfig fc = null;
public void destroy() {
System.out.println(" Filter  destory......");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("Filter do.......");
System.out.println(fc.getInitParameter("name") );
arg0.getRequestDispatcher("/MyJsp.jsp").forward(arg0, arg1);
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
fc = arg0;
System.out.println("Filter init......");
}
}

监听器:-----------------------------------------------------------------------------------------------------
实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。主要作用是: 做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/applicationContext*.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 监听器也属于servlet 规范

public class TestListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Listener distroy.............");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Listener init.............");
}
}

 拦截器:--------------------------------------------------------------------------------------------------

拦截器是在面向切面编程中应用的,就是在你的service或者一个方法前调用一个方法,或者在方法后调用一个方法。是基于JAVA的反射机制。拦截器不是在web.xml,比如struts在struts.xml中配置
<package name="global" namespace="/" extends="struts-default">
    <!-- struts2 拦截器Session 超时 -->
   <interceptors>
<interceptor name="sessionInterceptor" class="com.egintra.common.base.SessionInterceptor" />
<!-- 拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="sessionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
    <default-interceptor-ref name="myStack" />
</package>
拦截器是面向切面的应用,可以针对某一个方法进行拦截,sturts2中的体现:
public String intercept(ActionInvocation actionInvocation) throws Exception {
//cacheClean();
HttpSession session  = ServletActionContext.getRequest().getSession();
Action action = (Action) actionInvocation.getAction();
boolean loginFlag = false;
if(actionInvocation.getProxy().getMethod()!= null){
loginFlag = "userLogin".equals(actionInvocation.getProxy().getMethod());
}
if (action instanceof LoginAction && loginFlag) {
return actionInvocation.invoke();
}else{
SysUser user = UserSessionUtil.getUserInfo(session);
if (user == null) {
ServletActionContext.getRequest().setAttribute("message", "sessionLost");
return "login";
} else {
return actionInvocation.invoke();
}
}
}
原文地址:https://www.cnblogs.com/leonkobe/p/3519113.html