Activation and passivation of the JavaBean object of the Dark Horse DAY14 listener

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.