ServletContextListener、MyHttpSessionListener、ServletContextAttributeListener、对象绑定解绑、文件钝化活化
来源:互联网 发布:离线下载 知乎 编辑:程序博客网 时间:2024/04/29 05:22
ServletContextListener创建、销毁
配置文件配置入下:
<listener> <listener-class>com.itheima.create.MyServletContextListener</listener-class> </listener>
package com.itheima.create;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyServletContextListener implements ServletContextListener{ @Override //监听context域对象的创建 public void contextInitialized(ServletContextEvent sce) { //就是被监听的对象---ServletContext ServletContext servletContext = sce.getServletContext(); //getSource就是被监听的对象 是通用的方法 ServletContext source = (ServletContext) sce.getSource(); System.out.println("context创建了...."); //开启一个计息任务调度----每天晚上12点 计息一次 Timer timer = new Timer(); //task:任务 firstTime:第一次执行时间 period:间隔执行时间// timer.scheduleAtFixedRate(task, firstTime, period); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("银行计息了....."); } } , new Date(), 5000);} //监听context域对象的销毁 @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("context销毁了...."); }}
发布项目输出:context创建了….
定时器输出: 银行计息了…..
停止服务器:context销毁了….
MyHttpSessionListener创建、销毁
我们访问一个jsp页面,因为session是jsp页面的内置对象,所以第一次访问时候,默认创建session
首先需要配置如下:
<listener> <listener-class>com.itheima.create.MyHttpSessionListener</listener-class> </listener>
session.jsp
<body> xxxxxxxxxxxxxxxxxxxxxx</body>
package com.itheima.create;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class MyHttpSessionListener implements HttpSessionListener{ @Override public void sessionCreated(HttpSessionEvent se) { System.out.println("session创建"+se.getSession().getId()); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("session销毁"); }}
访问http://localhost:8080/WEB23/session.jsp
控制台输出:
session创建26F54F5E56E2EFB501DBE76A751C82DC
ServletContextAttributeListener修改、删除、添加
配置如下:
<listener> <listener-class>com.itheima.attribute.MyServletContextAttributeListener</listener-class> </listener>
package com.itheima.attribute;import javax.servlet.ServletContextAttributeEvent;import javax.servlet.ServletContextAttributeListener;public class MyServletContextAttributeListener implements ServletContextAttributeListener{ @Override public void attributeAdded(ServletContextAttributeEvent scab) { //放到域中的属性 System.out.println(scab.getName());//放到域中的name System.out.println(scab.getValue());//放到域中的value } @Override public void attributeRemoved(ServletContextAttributeEvent scab) { System.out.println(scab.getName());//删除的域中的name System.out.println(scab.getValue());//删除的域中的value } @Override public void attributeReplaced(ServletContextAttributeEvent scab) { System.out.println(scab.getName());//获得修改前的name System.out.println(scab.getValue());//获得修改前的value }}
看看servlet代码如下:
package com.itheima.attribute;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestMyServletContextAttributeListener extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); //向context域中存数据 context.setAttribute("name", "tom"); //改context数据 context.setAttribute("name", "lucy"); //删除context数据 context.removeAttribute("name"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
页面输入:http://localhost:8080/WEB23/test1
输出如下:
nametomnametomnamelucy
HttpSessionBindingListener绑定对象
绑定监听器无需再web.xml中进行配置
package com.itheima.domian;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;public class Person implements HttpSessionBindingListener{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override //绑定的方法 public void valueBound(HttpSessionBindingEvent event) { System.out.println("person被绑定了"); } @Override //解绑方法 public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("person被解绑了"); }}
package com.itheima.domian;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 TestPersonBindingServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //将person对象绑到session中 Person p = new Person(); p.setId("100"); p.setName("zhangsanfeng"); session.setAttribute("person", p); //将person对象从session中解绑 session.removeAttribute("person"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
浏览器输入:http://localhost:8080/WEB23/testBinding
输出如下:
person被绑定了person被解绑了
HttpSessionActivationListener钝化、活化
package com.itheima.domian;import java.io.Serializable;import javax.servlet.http.HttpSessionActivationListener;import javax.servlet.http.HttpSessionEvent;public class Customer implements HttpSessionActivationListener,Serializable{ private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override //钝化 public void sessionWillPassivate(HttpSessionEvent se) { System.out.println("customer被钝化了"); } @Override //活化 public void sessionDidActivate(HttpSessionEvent se) { System.out.println("customer被活化了"); }}
package com.itheima.domian;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 TestCustomerActiveServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //将customer放到session中 Customer customer =new Customer(); customer.setId("200"); customer.setName("lucy"); session.setAttribute("customer", customer); System.out.println("customer被放到session域中了"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
package com.itheima.domian;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 TestCustomerActiveServlet2 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session域中获得customer HttpSession session = request.getSession(); Customer customer = (Customer) session.getAttribute("customer"); System.out.println(customer.getName()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
http://localhost:8080/WEB23/testActive
然后停止服务器,使其出现钝化状态
然后在启动服务器,使其活化
我们可以在如下的文件中进行配置
<?xml version="1.0" encoding="UTF-8"?><Context> <!-- maxIdleSwap:session中的对象多长时间不使用就钝化 --> <!-- directory:钝化后的对象的文件写到磁盘的哪个目录下 配置钝化的对象文件在 work/catalina/localhost/钝化文件 --> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"> <Store className="org.apache.catalina.session.FileStore" directory="itheima32" /> </Manager></Context>
钝化的文件保存在下面:
阅读全文
0 0
- ServletContextListener、MyHttpSessionListener、ServletContextAttributeListener、对象绑定解绑、文件钝化活化
- 黑马day14 监听器之javaBean对象的活化和钝化
- 黑马day14 监听器之javaBean对象的活化和钝化
- session钝化与活化
- session钝化与活化
- ServletContextAttributeListener属性监听、ServletContextListener监听 ServletContext 对象的生命周期
- jQuery给对象绑定,解绑事件
- JavaWeb---session活化与钝化
- session的钝化和活化
- session的钝化与活化
- session的活化和钝化
- Session的活化与钝化
- HttpSessionActivationListener钝化与活化监听器
- javaEE之--------------session的活化与钝化
- Servlet监听器/Session钝化和活化
- session的钝化与活化技术
- IDEA之Session的活化和钝化
- HttpSessionListener,ServletContextListener, ServletContextAttributeListener,ServletContainer监听在线人数
- 使用IDLE查看python的内置函数的方法
- 将Eclipse代码导入到AndroidStudio的两种方式
- 流媒体协议与开源实现
- LoadRunner学习笔记——Day1
- CodeBlocks:for loop initial declarations are only allowed in c99 mode
- ServletContextListener、MyHttpSessionListener、ServletContextAttributeListener、对象绑定解绑、文件钝化活化
- git及gitlab的使用
- 设计模式学习-GOF结构型设计模式
- Shell字符串处理规律总结
- 基础篇:9)制造发展与对设计的要求
- 使用VBO对glVertexAttribPointer的影响
- Eclipse中svn设置忽视某个文件夹不提交
- Different Ways to Add Parentheses
- 009--swift基础语法(字典)