Activation and passivation of JavaBean objects:
If a property, such as String, is placed in the session field, or if a person class is placed in the session field, in order to implement the passivation and activation of the session, the JavaBean must implement the Serializable interface ...
1.javaBean person
package cn.itheima.domain;import Java.io.serializable;import Javax.servlet.http.httpsessionactivationlistener;import Javax.servlet.http.httpsessionbindingevent;import Javax.servlet.http.httpsessionbindinglistener;import Javax.servlet.http.httpsessionevent;public class Person Implements Serializable, httpsessionbindinglistener,httpsessionactivationlistener{private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} public void Valuebound (Httpsessionbindingevent event) {System.out.println ("when the current JavaBean object is bound to the session field, JavaBean perceives itself as being added to the session field, triggering this event ... "); public void Valueunbound (Httpsessionbindingevent event) {System.out.println ("when the current JavaBean object is removed from the Sessio domain, JavaBean perceives itself removed from the session field, triggering this event ... "); public void sessionwillpassivate (httpsessionevent se) {System.out.println ("JavaBean senses itself passivated ... Trigger this event ... "); public void sessiondidactivate (httpsessionevent se) {System.out.println ("JavaBean perceives itself to be activated ... Trigger this event ... ");}}
Here the listener does not have to be registered in the Web. xml config file ... You can use it directly
2.servlet placing instance P of the person class in a domain object
Package Cn.itheima.servlet;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;import Cn.itheima.domain.Person; public class Sessiondemo extends HttpServlet {public void doget (HttpServletRequest request, httpservletresponse response ) throws Servletexception, IOException {//Create Session object HttpSession session = Request.getsession (); Person P=new person (); Session.setattribute ("P", p);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Test: We first visit this servlet ....
If our person class implements the serializable interface ...
Gracefully shut down the server ....
Watch Tomcat working directory we found one more file: This file is a passivation file ... This is temporarily saved ... When the normal open, you will find the passivation file is loaded into the memory ...
Open Server normally
If the serializable interface is not implemented, events in JavaBean are not triggered .... That is, there is no passivation and activation of the said.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Activation and passivation of the JavaBean object of the Dark Horse DAY14 listener