Definition of monitoring
Listening to application.
Application is an object of the ServletContext interface, representing the entire context, and if you want to implement a application listener, you can use the following two interfaces:
Servletcontextlistener: A listener for the entire context
Servletcontextattrubitelistener: Is the listener for the property.
Importjavax.servlet.* ; Public classServletcontextlistenerdemoImplementsServletcontextlistener { Public voidcontextinitialized (Servletcontextevent event) {System.out.println ("* * Container Initialization--" +Event.getservletcontext (). Getcontextpath ()); } Public voidcontextdestroyed (Servletcontextevent event) {System.out.println ("* * Container destroyed--" +Event.getservletcontext (). Getcontextpath ()); Try{Thread.Sleep (300) ; }Catch(Exception e) {} }}
Configuring <listener> in Web. xml
If there is a servlet, filter, Listener in Web. XML, the configuration order is as follows
<filter>
<filter-mapping>
<listener>
<servlet>
<servlet-mapping>
Using the Servletcontextattributelistener Action property
Importjavax.servlet.* ; Public classServletcontextattributelistenerdemoImplementsServletcontextattributelistener { Public voidattributeadded (servletcontextattributeevent scab) {System.out.println (* * Add Properties--Property name: "+ scab.getname () +", Property content: "+Scab.getvalue ()); } Public voidattributeremoved (servletcontextattributeevent scab) {System.out.println ("* * Delete attribute--property name:" + scab.getname () + ", Property content:" +Scab.getvalue ()); } Public voidattributereplaced (servletcontextattributeevent scab) {System.out.println (* * Replace Property with property name: "+ scab.getname () +", Property content: "+Scab.getvalue ()); }}
Configuration in Web. xml
<listener><listener-class>org.lxh.listenerdemo.servletcontextattributelistenerdemo</ Listener-class></listener>
Shown in JSP:
<%request.setattribute ("info", "www.MLDNJAVA.cn");%>
monitoring of the session
Note: The session is part of the HTTP protocol category, so these interfaces are defined in Javax.servlet.http.
In a servlet, to get a session depends on the GetSession () method in the HttpServletRequest interface.
import javax.servlet.http.* ; Public class Implements Httpsessionlistener { publicvoid sessioncreated (httpsessionevent se) { SYSTEM.OUT.PRINTLN ("* * Session creation, Session ID =" +se.getsession (). GetId ()) ; } Public void sessiondestroyed (httpsessionevent se) { System.out.println ("* * Session destroyed, Session ID =" + Se.getsession (). GetId ());} }
After the Web. XML configuration, start the server
When a client connects to the server for the first time, the server automatically creates a session for the user and assigns a sessionid.
The session has been created, triggering an event on the first visit, but when is it logged off?
If you want the session to be unregistered, there are two methods, but neither of these methods can be achieved by closing the page.
Method 1:session Logoff---invaldate ();
Method 2: Time-out setting-the general time in Tomcat is set to 30min, or you can modify it yourself in Web. xml
<% // manual logoff session.invalidate (); %>
The session is destroyed when the invalidate () method is called.
Of course, the longer the time, the greater the memory consumption.
Settings in Web. xml
<session-config> <session-timeout>1</session-timeout></session-config>
listen to the properties in the session
Importjavax.servlet.http.* ; Public classHttpsessionattributelistenerdemoImplementsHttpsessionattributelistener { Public voidattributeadded (httpsessionbindingevent se) {System.out.println (Se.getsession (). GetId ()+ ", increase properties--property name" + se.getname () + ", attribute content:" +Se.getvalue ()); } Public voidattributeremoved (httpsessionbindingevent se) {System.out.println (Se.getsession (). GetId ()+ ", delete the attribute--property name" + se.getname () + ", attribute content:" +Se.getvalue ()); } Public voidattributereplaced (httpsessionbindingevent se) {System.out.println (Se.getsession (). GetId ()+ ", replace attribute--property name" + se.getname () + ", attribute content:" +Se.getvalue ()); }}
<% Session.setattribute ("info", "www.MLDNJAVA.cn"); %>
So far, all very similar to ServletContext, All of the previous snooping is configured in Web. XML, but there is a httpsessionbindinglistener in the session to implement the listening interface, but this interface does not need to be configured and can be used directly.
Importjavax.servlet.http.* ; Public classLoginuserImplementsHttpsessionbindinglistener {PrivateString name; PublicLoginuser (String name) { This. SetName (name); } Public voidValuebound (Httpsessionbindingevent event) {System.out.println (* * Save the Loginuser object in session (name = "+ This. GetName () + "), session id =" +event.getsession (). GetId ()); } Public voidValueunbound (Httpsessionbindingevent event) {System.out.println (* * Remove the Loginuser object from the session (name = "+ This. GetName () + "), session id =" +event.getsession (). GetId ()); } PublicString GetName () {return This. Name; } Public voidsetName (String name) { This. Name =name; }}
You do not need to configure Web. XML at this time, but the JSP is cumbersome.
<% new loginuser ("mldn"); Session.setattribute ("info", user); // save Loginuser Object directly %>
Listen to request
Importjavax.servlet.* ; Public classServletrequestlistenerdemoImplementsServletrequestlistener { Public voidrequestinitialized (servletrequestevent SRE) {System.out.println ("* * Request initialization. HTTP//"+sre.getservletrequest (). GETREMOTEADDR ()+Sre.getservletcontext (). Getcontextpath ()); } Public voidrequestdestroyed (servletrequestevent SRE) {System.out.println ("* * request destroyed. HTTP//"+sre.getservletrequest (). GETREMOTEADDR ()+Sre.getservletcontext (). Getcontextpath ()); }}
configuring in Web. xml
<listener> <listener-class> org.lxh.listenerdemo.ServletRequestListenerDemo </listen
Property operations on request
Importjavax.servlet.* ; Public classServletrequestattributelistenerdemoImplementsServletrequestattributelistener { Public voidattributeadded (servletrequestattributeevent srae) {System.out.println (* * Add Request Property--Property name: "+ srae.getname () +", Property content: "+Srae.getvalue ()); } Public voidattributeremoved (servletrequestattributeevent srae) {System.out.println (* * Delete Request Property--Property name: "+ srae.getname () +", Property content: "+Srae.getvalue ()); } Public voidattributereplaced (servletrequestattributeevent srae) {System.out.println (* * Replace the Request property with the property name: "+ srae.getname () +", Property content: "+Srae.getvalue ()); }}
In practice, the use of the request for monitoring is not much, the most used is the ServletContext and httpsession monitoring.
Listening Instances
ImportJava.util.* ;Importjavax.servlet.* ;Importjavax.servlet.http.* ; Public classOnlineuserlistImplementsServletcontextlistener,httpsessionattributelistener,httpsessionlistener {PrivateServletContext app =NULL ; Public voidcontextinitialized (Servletcontextevent sce) { This. App =Sce.getservletcontext (); This. App.setattribute ("online",NewTreeSet ());//Prepare the collection } Public voidcontextdestroyed (Servletcontextevent sce) {} Public voidattributeadded (httpsessionbindingevent se) {Set all= (Set) This. App.getattribute ("online") ; All.add (Se.getvalue ()); This. App.setattribute ("online", all); } Public voidattributeremoved (httpsessionbindingevent se) {Set all= (Set) This. App.getattribute ("online") ; All.remove (Se.getsession (). getattribute ("UserID")) ; This. App.setattribute ("online", all); } Public voidattributereplaced (httpsessionbindingevent se) {} Public voidsessioncreated (httpsessionevent se) {} Public voidsessiondestroyed (httpsessionevent se) {Set all= (Set) This. App.getattribute ("online") ; All.remove (Se.getsession (). getattribute ("UserID")) ; This. App.setattribute ("online", all); } }
<form action= "login.jsp" method= "POST" > user id: <input type= "text" Name= "userid" > <input type= "Submit" value= "Login" ></form><% String user ID = request.getparameter ("userid" if (! ( Userid==null | | "" .equals (userid)) {session.setattribute ( userid ,userid); Response.sendredirect ( "list.jsp" %>
<% this. Getservletcontext (). getattribute ("online"); = all.iterator (); while (Iter.hasnext ()) {%> }%>
servlet development in Javaweb--listener