其他監(jiān)聽器
監(jiān)聽者 | 被監(jiān)聽者 | 監(jiān)聽到事件對(duì)象 |
HttpsessionActivationListener | HttpSession – 監(jiān)聽HttpSession活化和頓化。 |
|
HttpSessionAttributeListener | HttpSession – 監(jiān)聽session的屬性變化的。S.setAttributee(); |
|
HttpSessionBindingListener | HttpSession - 監(jiān)聽哪一個(gè)對(duì)象,綁定到了session上。S.setAtrri(name,User); | |
HttpSessionListener | HttpSesion – 監(jiān)聽sessioin創(chuàng)建銷毀 |
|
ServletContextAttributeListener | ServletContext – 屬性變化的 | |
ServletContextListener[W1] | ServletContext 創(chuàng)建銷毀 | |
ServletRequestListener - SerlvetRequestAttibuteListner | Rrequest -創(chuàng)建銷毀 Request屬性變化 |
1ServletContextListener 用于監(jiān)聽SevletContext的創(chuàng)建
在web中的所的監(jiān)聽器都是全局的 - 都是在項(xiàng)目啟動(dòng)時(shí)直接由tomcat創(chuàng)建。
監(jiān)聽器沒有順序。只是監(jiān)聽的對(duì)象不一樣。
在一個(gè)項(xiàng)目中可以存在多個(gè)監(jiān)聽器。
package cn.hx.listener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PRintWriter;
import java.net.URL;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyContextListener implements ServletContextListener {
//在啟動(dòng)時(shí)讀取數(shù)據(jù)庫(kù)之前保存的數(shù)據(jù)
public void contextInitialized(ServletContextEvent sc) {
System.err.println("application被創(chuàng)建了:"+sc.getServletContext());
URL url = MyContextListener.class.getClassLoader().getResource("count.txt");
String path = url.getFile();
System.err.println(path);
try {
BufferedReader bf = new BufferedReader(new FileReader(path));
String line = bf.readLine();
Integer count = Integer.valueOf(line);
sc.getServletContext().setAttribute("count",count);
System.err.println("初始的值是:"+count);
} catch (Exception e) {
e.printStackTrace();
}
}
//在銷毀這個(gè)對(duì)象時(shí)保存一些數(shù)據(jù)到數(shù)據(jù)庫(kù)或是文件中
public void contextDestroyed(ServletContextEvent e) {
System.err.println("銷毀了:"+e.getServletContext());
//保存到文件中去
URL url = MyContextListener.class.getClassLoader().getResource("count.txt");
String path = url.getFile();
System.err.println(path);
File file = new File(path);
try {
PrintWriter out = new PrintWriter(file);
//獲取applicat的數(shù)據(jù)
Integer count = (Integer) e.getServletContext().getAttribute("count");
out.print(count);
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
在項(xiàng)目啟動(dòng)時(shí)一次加載所有的配置文件。
2 監(jiān)聽一個(gè)Sesison被保存到一個(gè)文件中的過程第一步:書寫B(tài)ean實(shí)現(xiàn)HttpSessionActivationListener
package cn.hx.domain;
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
public class Person implements Serializable,HttpSessionActivationListener{
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sessionWillPassivate(HttpSessionEvent se) {
System.err.println("保存到文件中去了..."+this.getName());
}
public void sessionDidActivate(HttpSessionEvent se) {
System.err.println("從文件中活化了...."+this.getName());
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
第二步:配置這個(gè)項(xiàng)目
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注