Javaweb Series (listener)

Source: Internet
Author: User

1. Listener: Event source Listener State Listener event on event Source: When the event source changes, the trigger listener 2.servlet listener servlet is divided into three categories: the state of the inside domain object is being monitored ServletContext httpsess Ion,servletrequestCategory One: monitoring the creation and destruction of three domain objectsServletContext: Entire Web project, created at server startup, destroyed when server shuts downthe first step: Implement an interface Servletcontextlistener method to implement it contextinitialized (Servletcontextevent SCE): This method is automatically executed if a domain object is createdcontextdestroyed (Servletcontextevent SCE): This method is automatically executed if the domain object is destroyedSecond step: Configure in Web. XML to let the server know that it is not an ordinary Java program, but a listener <listener> <listener-class>cn.itcast . Listener. testservletcontext</listener-class> </listener> ServletContext domain applications: Because they are created when the server is started, they are often used to create database connection pools , or load the configuration file when the server starts HttpSession one session, request.getseesion create. Three kinds of destruction (1) Abnormal shutdown server (2) Default expiration Time (3) Call method The first step is to achieve:Httpsessionlistener Interface when you visit a JSP, a session is created because it compiles into a servlet,jsp with built-in objects, GetSessionAccessing a servlet does not create a session only when it accesses getsession and createsthe Session object is not created when the HTML is accessed. Step two configure Web. xmlServletRequest One request, created at request, end of request    ListeningServletrequestlistenerThere are two methods that represent the time that the domain object was created to executeaccessing jsp,servlet,html creates a request object that will be created whenever the browser is a request     Class II: Listen to the changes in the properties of the three domain objects (add, replace, remove) to monitor the changes of the attributes inside the ServletContext domain First step: Implement the Servletcontextattributelistener interfaceThere are three methods in the interface-attributeadded (Servletcontextattributeevent event): Add attributes to the domain object, execute this method-Attributeremove D (Servletcontextattributeevent event): Removes the attribute from the domain object and executes this method-attributereplaced (Servletcontextattributeevent event ): Replace the value of the property inside the domain object, the second operation is configured in Web. XML to let the server know that a listener listens for changes in the properties of the HttpSession domainFirst step: Implement the Httpsessionattributelistener interface There are three methods in the interface-attributeadded (Httpsessionbindingevent Event): Add properties to the domain object, execute this method-attributeremoved (H Ttpsessionbindingevent event): Removes the attribute from the domain object and executes this method-attributereplaced (Httpsessionbindingevent event): Replaces the Field object property, the second operation is configured in Web. XML to let the server know that it is a listener
Listen for changes in the properties of the ServletRequest domain First step: Implement the Servletrequestattributelistener interfaceThere are three methods in the interface-attributeadded (servletrequestattributeevent srae): Add properties to the domain object, execute this method-attributeremoved ( Servletrequestattributeevent srae): Removes the attribute from the domain object and executes this method-attributereplaced (Servletrequestattributeevent srae): The value of the property inside the domain object is changed, this method is executedhow to get ServletContext object in ServletContext listenerby Servletcontextevent inside the Getservletcontext () method to get
Category three: Monitoring the state of the JavaBean inside the HttpSessionFirst Interface Httpsessionbindinglistener interfaceThere are two methods inside the interface -Valuebound (Httpsessionbindingevent event) Binding: Place the JavaBean object inside the Session Field object-Valueunbound (Httpsessionbindingevent event) Unbind: Remove the JavaBean object from the session fieldThe second interface Httpsessionactivationlistener interface has two methods -sessiondidactivate (httpsessionevent se)Activation-sessionwillpassivate (httpsessionevent se) activation: Deserializes the serialized session of the disk into the project Passivation: Serialize session to diskGracefully shut down the server, in the work directory there is serialized file implementation interface Serializable class can be serialized, the user does not have this interface before the exception and map has the inheritance of serialization
not a servlet can't use Getservletcontext to get ServletContext.4. Pagination Display of dataThe first way: Logical paging (false paging)Once the data are taken out, each display part, disadvantage: large data volume, long time, advantages: only need to interact with the database once,,second way: Physical pagingEach time according to the displayed page and data, to the database to query the need for multiple and data interaction can use the masql inside the keyword, limit implementation paging, the starting position how to calculate: (Current page-1) * Number of records per page start position currently displayed recordsfirst page 0 5 Total pages how to record: Total number of records/pages of records if the total number of records can be divided evenly, the page is the result if the total number of records can not be divisible, the page is the result of the addition of +1 first create JavaBean in the attribute













Javaweb Series (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.