标签:character tin pac 过程 文件中 xml文件 extends servlet 就会
1. 什么是监听器
2.监听器最常见的应用场景:
3. 监听器术语
4. 监听器的编写流程
5. JavaEE规范规定了8个监听器接口用于监听不同的web对象
6. 域对象监听器
7. 域对象属性监听器(
4大域对象:ServletContext 、Request、Session、PageContext
7.1 作用域属性操作
setAttribute(k,v) getAttribute(k) removeAttribute(k)
7.2 域对象属性变更监听器
7.3 特殊的Javabean监听器(Session作用域)
前面六个监听器需要在web.xml中配置,最后的两个监听器不需要再web.xml中配置
8. 实例
8.1 ServletRequestListener
监听request对象创建或销毁,其中request对象的特点如下:
1 package com.eagle.listener; 2 3 import javax.servlet.ServletRequestEvent; 4 import javax.servlet.ServletRequestListener; 5 6 public class MyServletRequestListener implements ServletRequestListener { 7 8 public MyServletRequestListener() { 9 super(); 10 System.out.println("request对象的构造方法"); 11 } 12 13 public void requestInitialized(ServletRequestEvent sre) { 14 System.out.println("request创建了!"); 15 } 16 17 public void requestDestroyed(ServletRequestEvent sre) { 18 System.out.println("request对象销毁了!"); 19 } 20 21 }
1 <listener> 2 <listener-class>com.eagle.listener.MyServletRequestListener</listener-class> 3 </listener>
服务器启动就先构造了request对象,然后每个页面请求都会触发
MyServletRequestListener.requestInitialized(ServletRequestEvent sre)方法和
MyServletRequestListener.requestDestroyed(ServletRequestEvent sre)方法
8.2 HttpSessionListener
1 package com.eagle.listener; 2 3 import javax.servlet.http.HttpSessionEvent; 4 import javax.servlet.http.HttpSessionListener; 5 6 public class MyHttpSessionListener implements HttpSessionListener { 7 8 public MyHttpSessionListener() { 9 10 super(); 11 System.out.println("session对象构造方法"); 12 } 13 14 public void sessionCreated(HttpSessionEvent arg0) { 15 System.out.println("创建session对象"); 16 } 17 18 public void sessionDestroyed(HttpSessionEvent arg0) { 19 System.out.println("销毁session对象"); 20 } 21 22 }
1 package com.eagle.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 public class CreateSession extends HttpServlet { 11 12 private static final long serialVersionUID = 1L; 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 request.setCharacterEncoding("utf-8"); 16 response.setContentType("text/html;charset=utf-8"); 17 HttpSession httpSession = request.getSession(); 18 httpSession.setMaxInactiveInterval(10); 19 String id = httpSession.getId(); 20 if(httpSession.isNew()) { 21 httpSession.setAttribute("name", "狼"); 22 String name = (String) httpSession.getAttribute("name"); 23 response.getWriter().print("恭喜session创建成功,sessionid="+id+";name="+name); 24 }else { 25 String name = (String) httpSession.getAttribute("name"); 26 response.getWriter().print("服务器已经存在session了,sessionid="+id+";name="+name); 27 } 28 } 29 30 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 doGet(request, response); 32 } 33 }
1 <listener> 2 <listener-class>com.eagle.listener.MyHttpSessionListener</listener-class> 3 </listener>
8.3 servletContextListener
1 package com.eagle.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 6 public class MyServletContextListener implements ServletContextListener { 7 8 public MyServletContextListener() { 9 System.out.println("ServletContext对象构建!"); 10 } 11 12 public void contextInitialized(ServletContextEvent sce) { 13 System.out.println("ServletContext对象初始化!"); 14 } 15 16 public void contextDestroyed(ServletContextEvent sce) { 17 System.out.println("ServletContext对象销毁!"); 18 } 19 }
1 <listener> 2 <listener-class>com.eagle.listener.MyServletContextListener</listener-class> 3 </listener>
标签:character tin pac 过程 文件中 xml文件 extends servlet 就会
原文地址:https://www.cnblogs.com/eaglesour/p/9471060.html