标签:绑定 enc apache 方法 技术 ted 操作 .com nbsp
1.listener的重要概念
ServletContextListener 监听ServletContext的创建和销毁
2.使用步骤
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <listener> 4 <listener-class>listener.ApplicationInitListener</listener-class> 5 </listener> 6 </web-app>
1 package listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 6 public class ContextListener implements ServletContextListener { 7 8 @Override 9 public void contextDestroyed(ServletContextEvent arg0) { 10 // TODO Auto-generated method stub 11 System.out.println("ServletContext域对象销毁了"); 12 } 13 14 @Override 15 public void contextInitialized(ServletContextEvent arg0) { 16 // TODO Auto-generated method stub 17 System.out.println("ServletContext域对像创建了"); 18 } 19 20 }
1 package domain; 2 3 import javax.servlet.http.HttpSessionActivationListener; 4 import javax.servlet.http.HttpSessionBindingEvent; 5 import javax.servlet.http.HttpSessionEvent; 6 7 public class User implements HttpSessionActivationListener { 8 private String username; 9 private String password; 10 11 public String getUsername() { 12 return username; 13 } 14 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 19 public String getPassword() { 20 return password; 21 } 22 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 27 public static void main(String[] args) { 28 System.out.println(User.class.getName()); 29 } 30 31 @Override 32 public void sessionDidActivate(HttpSessionEvent arg0) { 33 // TODO Auto-generated method stub 34 System.out.println("很久没有使用了,写入磁盘了"); 35 } 36 37 @Override 38 public void sessionWillPassivate(HttpSessionEvent arg0) { 39 // TODO Auto-generated method stub 40 System.out.println("用户使用了,我又回到内存了"); 41 } 42 43 }
<?xml version="1.0" encoding="UTF-8"?> <Context> <!-- maxIdleSwap :1分钟 如果session不使用就会序列化到硬盘. directory :itheima 序列化到硬盘的文件存放的位置. --> <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1"> <Store className="org.apache.catalina.session.FileStore" directory="itheima" /> </Manager> </Context>
1 package domain; 2 3 import javax.servlet.http.HttpSessionBindingEvent; 4 import javax.servlet.http.HttpSessionBindingListener; 5 6 public class User implements HttpSessionBindingListener { 7 private String username; 8 private String password; 9 10 public String getUsername() { 11 return username; 12 } 13 14 public void setUsername(String username) { 15 this.username = username; 16 } 17 18 public String getPassword() { 19 return password; 20 } 21 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 26 public static void main(String[] args) { 27 System.out.println(User.class.getName()); 28 } 29 30 @Override 31 public void valueBound(HttpSessionBindingEvent arg0) { 32 // TODO Auto-generated method stub 33 System.out.println("我被添加到Session中啦~~~~~~~"); 34 } 35 36 @Override 37 public void valueUnbound(HttpSessionBindingEvent arg0) { 38 // TODO Auto-generated method stub 39 System.out.println("我从Session中被移除了~~~~~~~"); 40 } 41 42 }
标签:绑定 enc apache 方法 技术 ted 操作 .com nbsp
原文地址:http://www.cnblogs.com/jimisun/p/7818029.html