JavaEE基础(05):过滤器、监听器、拦截器,应用详解

指被监听对象。

ServletContext

ServletContextListener生命周期监听,它有两个方法,出生时调用contextInitialized(),销毁时调用contextDestroyed();

ServletContextAttributeListener属性监听,它有三个方法,添加属性attributeAdded(),替换属性attributeReplaced(),移除属性时attributeRemoved()。

HttpSession

HttpSessionListener生命周期监听:它有两个方法,出生时调用sessionCreated(),销毁时调用sessionDestroyed();

HttpSessioniAttributeListener属性监听:它有三个方法,添加属性attributeAdded(),替换属性attributeReplaced(),移除属性attributeRemoved()。

ServletRequest

ServletRequestListener生命周期监听:它有两个方法,出生时调用requestInitialized(),销毁时调用requestDestroyed();

ServletRequestAttributeListener属性监听:它有三个方法,添加属性attributeAdded(),替换属性attributeReplaced(),移除属性attributeRemoved()。

3、编码案例

相关监听器

TheContextListener

public class TheContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("初始化:TheContextListener"); ServletContext servletContext = servletContextEvent.getServletContext() ; servletContext.setAttribute("author","cicada"); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("销毁:TheContextListener"); } }

TheRequestListener

public class TheRequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent servletRequestEvent) { System.out.println("初始化:TheRequestListener"); } @Override public void requestInitialized(ServletRequestEvent servletRequestEvent) { System.out.println("销毁:TheRequestListener"); } }

TheSessionListener

public class TheSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("初始化:TheSessionListener"); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("销毁:TheSessionListener"); } }

RequestAttributeListener

public class RequestAttributeListener implements ServletRequestAttributeListener { @Override public void attributeAdded(ServletRequestAttributeEvent evt) { System.out.println("Request添加属性:"+evt.getName()+";"+evt.getValue()); } @Override public void attributeRemoved(ServletRequestAttributeEvent evt) { System.out.println("Request移除属性:"+evt.getName()+";"+evt.getValue()); } @Override public void attributeReplaced(ServletRequestAttributeEvent evt) { System.out.println("Request替换属性:"+evt.getName()+";"+evt.getValue()); } }

web.xml配置文件

<!-- 监听器相关配置 --> <listener> <listener-class>com.node05.servlet.listener.TheContextListener</listener-class> </listener> <listener> <listener-class>com.node05.servlet.listener.TheSessionListener</listener-class> </listener> <listener> <listener-class>com.node05.servlet.listener.TheRequestListener</listener-class> </listener> <listener> <listener-class>com.node05.servlet.listener.RequestAttributeListener</listener-class> </listener> <session-config> <!-- 设置session失效时间为1分钟 --> <session-timeout>1</session-timeout> </session-config>

测试接口

public class ListenerServletImpl extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); // 1、获取TheContextListener初始化数据 ServletContext servletContext = this.getServletContext() ; String author = String.valueOf(servletContext.getAttribute("author")) ; System.out.println("TheContextListener Author:"+author); // 2、Request属性设置 request.setAttribute("mood","smile"); request.setAttribute("mood","agitated"); // 3、Session创建,1分钟失效,调用销毁 HttpSession session = request.getSession(true) ; session.setAttribute("casually","casually"); response.getWriter().print("Hello:Listener"); } } 二、Filter过滤器 1、过滤器简介

客户端请求Servlet时,先执行相关Filter,如果Filter通过,则继承执行请求的Servlet;如果Filter不通过,则不会执行用户请求的Servlet。过滤器可以动态地拦截请求和响应。

2、Filter接口

Filter接口定义了三个核心方法。

init()

应用程序启动时,服务器实例化Filter对象,并调用其init方法,读取web.xml配置,完成对象的初始化加载。

doFilter()

实际的过滤操作,请求达到服务器时,Servlet容器将先调用过滤器的doFilter方法。

destroy()

容器在销毁过滤器前调用该方法,释放过滤器占用的资源。

3、编码案例

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zydpgs.html