jsp-3 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里

那个先注册哪个先过滤的

posted @ 2017-01-17 23:22  lgp20151222  阅读(212)  评论(0编辑  收藏  举报