session activation and passivation is abnormal when a user visits the site, can not lose session, must adopt all files are stored; and before the traffic statistics site same principle.
class Person implementsHttpSessionActivationListener,Serializable
We must implement these two interfaces, to achieve session activation and deactivation required
Activation: reading from the hard disk into memory
Purification: written from memory on the hard disk
HttpSessionActivationListener
Implementation of this interface JavaBean, it has been activated can sense (from the hard disk memory), and a passivation process (from the memory to the hard disk) is.
If you need to save Session of the JavaBean JavaBean serialization must implement the Serializable interface.
Implementation of this interface JavaBean and HttpSessionBindingListener as necessary to configure the web.xml.
import java.io.Serializable;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
public class Person implements HttpSessionActivationListener,Serializable{
private static final long serialVersionUID = 1L;
private String name;
public Person(String name) {
this.name = name;
}
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("一个Person对象保存到硬盘了...");
}
@Override
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("一个Person对象从硬盘读取出来了...");
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
It is nothing after this realization, but also requires a configuration file.
Configuration files can be written in the directory conf tomcat's --- "server.xml configuration inside, but there will modify the platform configuration,
It is not recommended. Another way: We conf ---- "Catalina ------" localhost ------ "a custom xml file, the contents of which:
<Context path="/sessionActivation" docBase="E:/MyEclipse10_workspace/sessionActivation/WebRoot">
<Manager className="org.apache.catalina.session.PersistentManager"
saveOnRestart="true" maxActiveSessions="1">
<Store className="org.apache.catalina.session.FileStore" directory="d:/a">
</Store>
</Manager>
</Context>
<!-- 以下是对上面的解释 -->
<!-- path为项目的目录 docbase 为myeclipse目录
className 不能变
saveOnRestart ="true" 能够进行存储
maxActiveSessions="n" n为最大的session数量
-->
This can be achieved activation of the session.
Own summary:
Server restart to save and restore user session information (server maintenance needs)
httpsessionActivationlistener
Activation (sessiondidactivate) and a passivation sessionWillpassivate
public class Person httpsessionactivationlistener the implements, serviliziable (serializable, persistent storage) {
Activation and inactivation method "main xml configuration file or"
}
types in the listener's WEB: ServletContextListener (maximum)
HttpSessionListener (session to create access, destruction) (onlineweb)
activation and passivation need to configure the tomcat server (1, servce.xml --- 2, work catalina custom .xml file)
Configuration path --- D: \ javaSoft \ apache-tomcat-7.0.30 \ conf \ Catalina \ localhost need to write an xml file,
Let the server boot time to readOr configure server.xml (<Host name = "localhost" appBase = "webapps") to change the internet
Custom xml file :( do not publish) sess.xml teacher wrote
<Context path = "/ project name" docbase = "webroot Right + location" E: \ javaEcilpose \ onlineWeb \ WebRoot "">
<manager classname="org.apache.catalina.session.PersistentManager" saveOnRestart="true"《是否保存》 maxActiveSessions="1" 《最大的session 》>
<Store className = "org.apache.catalina.session.FileStore" "not change" directory = "d: / a" "storage path">
Custom xml file end
Direct access to 8080 / sess.xml direct access
session is necessary to activate or passive, mainly about the configuration file sess.xml