专栏首页lgp20151222filter和listener的生命周期

filter和listener的生命周期

filter(过滤器)和listener(监听器)的生命周期

老实说

生命周期要是说成作用的时间范围我会理解的更好

filter

package com.javaweb.filter;

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;

public abstract class HttpFilter implements Filter {

    private FilterConfig filterConfig;
    
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
            throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletrequest;
        HttpServletResponse response=(HttpServletResponse)servletresponse;
        doFilter(request,response,filterchain);
    }
    
    public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterchain)
            throws IOException, ServletException ;

    @Override
    public void init(FilterConfig filterconfig) throws ServletException {
        this.filterConfig=filterconfig;
        init();
    }
    
    public FilterConfig getFilterConfig() {
        return filterConfig;
    }
    
    public void init() throws ServletException{
        
    }

}
package com.javaweb.login;

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 LoginFilter extends HttpFilter {

    public void destroy() {
        // TODO Auto-generated method stub
        System.out.println("离开LoginFilter");
    }

    public void init() throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("初始化LoginFilter 只会出现一次!");
    }

    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterchain)
            throws IOException, ServletException {
        System.out.println("进入LoginFilter");
        filterchain.doFilter(request, response);//放行
        System.out.println("离开LoginFilter");
    }
}

listener

package com.javaweb.listener;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class MyHttpRequestListener implements ServletRequestListener,ServletRequestAttributeListener {

    @Override
    public void requestDestroyed(ServletRequestEvent servletrequestevent) {
        System.out.println("requestDestroyed...");
    }

    @Override
    public void requestInitialized(ServletRequestEvent servletrequestevent) {
        System.out.println("requestInitialized...");
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletrequestattributeevent) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletrequestattributeevent) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletrequestattributeevent) {
        // TODO Auto-generated method stub
        
    }

}

servlet

package com.javaweb.login;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = -6820554522964310475L;
    
    private String userName;
    
    private String password;
    
    public void init(){
        userName=getServletConfig().getInitParameter("username");
        password=getServletConfig().getInitParameter("password");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String pwd=req.getParameter("password");
        if(username!=null && !"".equals(username) && pwd!=null && !"".equals(pwd)){
            if(userName.equals(username) && password.equals(pwd)){
                HttpSession session=req.getSession();
                session.setAttribute("username", username);
                session.setAttribute("password", pwd);
                resp.sendRedirect(req.getContextPath()+"/login.jsp");
            }else{
                resp.sendRedirect(req.getContextPath()+"/index.jsp");
            }
        }
    }
}

filiter在服务器第一次加载时就运行一次init

而监听器每一次request请求就会运行一次init

但在响应时

监听器先得到响应

过滤器后得到响应

(在实际中,是并发执行,只是过滤器慢一点)

过滤器的过滤顺序是按web.xml里

那个先注册哪个先过滤的

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 用cookie实现记住密码

    ydymz
  • ThreadLocal基本原理及运用

    ThreadLocal提供本地线程变量。这个变量里面的值(通过get方法获取)是和其他线程分割开来的,变量的值只有当前线程能访问到,不像一般的类型比如Perso...

    ydymz
  • …… are only available on JDK 1.5 and higher 错误(spring 的jdk版本检测在jdk 8下的修订)

    1、在项目中创建一个package为org.springframework.core 2、在该package下面新建JdkVersion.java,内容如下...

    ydymz
  • Spring的三大器之监听器

    现在来说说Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是...

    一觉睡到小时候
  • 【原创】Spring Boot 过滤器、监听器、拦截器的使用

    在开发中用到过滤器、监听器、拦截器的场景非常多,今天就来聊聊这三者在日常开发中是如何使用的。

    田维常
  • springboot开发之配置Servlet三大组件(Servlet、Filter、Listener)

    1、首先我们自己定义三大组件:MyServlet.java、MyFilter.java、MyListener.java

    绝命生
  • Springboot笔记~filter

    Springboot默认启用了三个filter,他们是requestContextFilter,characterEncodingFilter,formCont...

    用户4056126
  • Servlet学习小结

    黑泽君
  • Java工具类- 跨域工具类

    原本Spring MVC项目添加跨域: 在web.xml文件中配置: <!-- cors解决跨域访问问题 --> <filter> ...

    二十三年蝉
  • Servlet总结五(监听器)

    生命周期监听器 生命周期就是从创建到销毁,因此顾名思义,就是在对象创建的时候会触发,销毁的时候也会触发

    爱撒谎的男孩

扫码关注云+社区

领取腾讯云代金券