Listen to SevletContext creation and listen to the process of saving a Sesison to a file (57), context
Other listeners
Listener |
Listener |
Event object monitored |
HttpSessionActivationListener |
HttpSession-Listen for HttpSession activation and pause. |
HttpSessionEvent
|
HttpSessionAttributeListener |
HttpSession-listens for changes in the attributes of a session. S. setAttributee (); |
HttpSessionBindingEvent
|
HttpSessionBindingListener |
HttpSession-which object to listen to is bound to the session. S. setAtrri (name, User ); |
|
HttpSessionListener |
HttpSesion-listening for sessioin creation and destruction |
HttpSessionEvent
|
ServletContextAttributeListener |
ServletContext-attributes changed |
|
ServletContextListener[W1] |
ServletContext creation and destruction |
|
ServletRequestListener-SerlvetRequestAttibuteListner |
Rrequest-create and destroy Request attribute change |
|
1 ServletContextListener is used to listen for SevletContext Creation
All listeners on the web are global-all are directly created by tomcat when the project is started.
The listener has no order. Only the listening objects are different.
Multiple listeners can exist in a project.
PackageCn. hx. listener;
ImportJava. io. BufferedReader;
ImportJava. io. File;
ImportJava. io. FileNotFoundException;
ImportJava. io. FileReader;
ImportJava. io. PrintWriter;
ImportJava.net. URL;
ImportJavax. servlet. ServletContextEvent;
ImportJavax. servlet. ServletContextListener;
Public ClassMyContextListenerImplementsServletContextListener {
// Read the data stored before the database at startup
Public VoidContextInitialized (ServletContextEvent SC ){
System.Err. Println ("application created:" + SC. getServletContext ());
URL url = MyContextListener.Class. GetClassLoader (). getResource ("count.txt ");
String path = url. getFile ();
System.Err. Println (path );
Try{
BufferedReader bf =NewBufferedReader (NewFileReader (path ));
String line = bf. readLine ();
Integer count = Integer.ValueOf(Line );
SC. getServletContext (). setAttribute ("count", count );
System.Err. Println ("Initial Value:" + count );
}Catch(Exception e ){
E. printStackTrace ();
}
}
// Save some data to the database or file when this object is destroyed
Public VoidContextDestroyed (ServletContextEvent e ){
System.Err. Println ("destroyed:" + e. getServletContext ());
// Save it to the file
URL url = MyContextListener.Class. GetClassLoader (). getResource ("count.txt ");
String path = url. getFile ();
System.Err. Println (path );
File file =NewFile (path );
Try{
PrintWriter out =NewPrintWriter (file );
// Obtain applicat data
Integer count = (Integer) e. getServletContext (). getAttribute ("count ");
Out. print (count );
Out. close ();
}Catch(FileNotFoundException e1 ){
E1.printStackTrace ();
}
}
}
Load all configuration files at one time when the project starts.
2. Listen to the process of saving a Sesison to a file.
Step 1: Write Bean implementationHttpSessionActivationListener
PackageCn. hx. domain;
ImportJava. io. Serializable;
ImportJavax. servlet. http. HttpSessionActivationListener;
ImportJavax. servlet. http. HttpSessionEvent;
Public ClassPersonImplementsSerializable, HttpSessionActivationListener {
PrivateString name;
PublicPerson (){
}
PublicPerson (String name ){
This. Name = name;
}
PublicString getName (){
ReturnName;
}
Public VoidSetName (String name ){
This. Name = name;
}
Public VoidSessionWillPassivate (HttpSessionEvent se ){
System.Err. Println ("saved to file..." +This. GetName ());
}
Public VoidSessionDidActivate (HttpSessionEvent se ){
System.Err. Println ("activated from the file..." +This. GetName ());
}
@ Override
PublicString toString (){
Return"Person [name =" + name + "]";
}
}
Step 2: configure this project
<Context docBase = "D :\\ programfiles \ MyEclipse10 \ wk3 \ WebRoot">
<Manager className = "org. apache. catalina. session. PersistentManager"
SaveOnRestart = "true">
<Store className = "org. apache. catalina. session. FileStore"
Directory = "d:/a">
</Store>
</Manager>
</Context>
Step 3: Test
<%
If(Session. getAttribute ("p") =Null){
IntA =NewRandom (). nextInt (100 );
Person p =NewPerson ("" + );
Session. setAttribute ("p", p );
}
// Save the cookie
Cookie c =NewCookie ("JSESSIONID", session. getId ());
C. setMaxAge (60*30 );
C. setPath (request. getContextPath ());
Response. addCookie (c );
%>
<Hr/>
$ {P}
<Hr/>
<% = Session. getId () %>