Java basics --- Servlet listener, basics --- servlet

Source: Internet
Author: User

Java basics --- Servlet listener, basics --- servlet

Before writing this article, read the school friends write, I feel the basic knowledge of induction is very comprehensive, I am not cumbersome to write here, link address (http://www.cnblogs.com/sherryueda/p/4273169.html ),

I will write down the specific application of the listener:

The function is used to listen to various WEB operations. When an event is triggered, an event is generated and processed. The application, session, and request operations can be monitored on the WEB.

Listening to application: Listening to application is actually listening to ServletContext (Servlet context). It mainly uses the following two interfaces: ServletContextListener and ServletContextAttributeListener.
Package com. oumyye. listener; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; public class ServletContextListenerDemo implements ServletContextListener {public void contextInitialized (ServletContextEvent event) {// System is triggered during context initialization. out. println ("** container initialization -->" + event. getServletContext (). getContextPath ();} public void contextDestroyed (ServletContextEvent event) {// System is triggered when the context is destroyed. out. println ("** container destruction -->" + event. getServletContext (). getContextPath ());}}

Web. xml configuration

<Listener> <listener-class> com. oumyye. listener. ServletContextListenerDemo </listener-class> </listener>
The session listener provides three interfaces for session listening: HttpSessionListener, HttpSessionAttributeListener, and HttpSessionBindingListenersession status listener: HttpSessionListener.
  • Javax can be implemented when you need to listen on the operation of creating or destroying sessions. servlet. http. httpSessionListener interface, which is defined as follows: public void sessionCreated (HttpSessionEvent se), public void sessionDestroyed (HttpSessionEvent se)
  • After a session is created or destroyed, an HttpSessionEvent is generated. The operation defined for this event is as follows: public HttpSession getSession ()

Listen to sessions

Package com. oumyye. listener; import javax. servlet. http. httpSessionEvent; import javax. servlet. http. httpSessionListener; public class HttpSessionListenerDemo implements HttpSessionListener {public void sessionCreated (HttpSessionEvent event) {// creates a session to trigger System. out. println ("** SESSION creation, session id =" + event. getSession (). getId ();} public void sessionDestroyed (HttpSessionEvent event) {// destroy the session to trigger the System. out. println ("** SESSION destruction, session id =" + event. getSession (). getId ());}}

Web. xml configuration

<Listener> <listener-class> com. oumyye. listener. HttpSessionListenerDemo </listener-class> </listener>

Session destruction

When a new user opens a dynamic page, the server allocates a session for the new user and triggers the sessionCreated () event in the HttpSessionListener interface, however, there are two different ways to trigger the sessionDestroyed () event during user destruction: Method 1: Call the invalidate () method of the HttpSession interface to invalidate a session. Method 2: exceeds the configured session timeout and session Timeout, which can be configured directly in web. xml of the project. Session attribute listening: The HttpSessionAttributeListener interface can also listen on session attribute operations in session listening. This is the same as listening on context attributes. You need to listen on session attribute operations, you can use javax. servlet. http. the authorization interface is complete. The method of this interface is as follows: public void attributeAdded (HttpSessionBindingEvent se), public void reset (HttpSessionBindingEvent se), public void attributeReplaced (HttpSessionBindingEvent se, the method in the HttpSessionAttributeListener interface is triggered based on the operation of the attribute. Each operation method generates an HttpSessionBindingEvent event. The operation is defined as follows: public HttpSession getSession (), public String getName (), public Object getValue () listens to session attribute operations:
Package com. oumyye. listener; import javax. servlet. http. httpSessionAttributeListener; import javax. servlet. http. httpSessionBindingEvent; public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener {public void attributeAdded (HttpSessionBindingEvent event) {// call System when the attribute is added. out. println (event. getSession (). getId () + ", add Property --> property name:" + event. getName () + ", attribute content:" + event. getValue ();} public void attributeRemoved (HttpSessionBindingEvent event) {// call System when the attribute is deleted. out. println (event. getSession (). getId () + ", delete attribute --> attribute name:" + event. getName () + ", attribute content:" + event. getValue ();} public void attributeReplaced (HttpSessionBindingEvent event) {// call System when the attribute is replaced. out. println (event. getSession (). getId () + ", replace Attributes --> attribute name:" + event. getName () + ", attribute content:" + event. getValue ());}}

Web. xml configuration

<Listener> <listener-class> com. oumyye. listener. HttpSessionAttributeListenerDemo </listener-class> </listener>
Session attribute listening: The HttpSessionBindingListener interface can also listen on session attribute operations in session listening. This is the same as listening on the context attributes. You need to listen on session attribute operations, you can use javax. servlet. http. httpSessionAttributeListener interface: public void attributeAdded (HttpSessionBindingEvent se) public void attributeRemoved (HttpSessionBindingEvent se) public void attributeReplaced (HttpSessionBindingEvent se) When performing attribute operations, the method in the HttpSessionAttributeListener interface will be triggered Based on the attribute operation. Each operation method will generate the HttpSessionBindingEvent event public HttpSession getSession () public String getName () public Object getValue () Listener for the session attribute operation.
Package com. oumyye. listener; import javax. servlet. http. httpSessionAttributeListener; import javax. servlet. http. httpSessionBindingEvent; public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener {public void attributeAdded (HttpSessionBindingEvent event) {// call System when the attribute is added. out. println (event. getSession (). getId () + ", add Property --> property name:" + event. getName () + ", attribute content:" + event. getValue ();} public void attributeRemoved (HttpSessionBindingEvent event) {// call System when the attribute is deleted. out. println (event. getSession (). getId () + ", delete attribute --> attribute name:" + event. getName () + ", attribute content:" + event. getValue ();} public void attributeReplaced (HttpSessionBindingEvent event) {// call System when the attribute is replaced. out. println (event. getSession (). getId () + ", replace Attributes --> attribute name:" + event. getName () + ", attribute content:" + event. getValue ());}}

Same as web. xml

Session property listening: The HttpSessionBindingListener interface also provides a javax on the WEB. servlet. http. httpSessionBindingListener interface, through which the listener can be directly used without configuration. This interface is defined as follows: public void valueBound (HttpSessionBindingEvent event) public void valueUnbound (HttpSessionBindingEvent event) the request listener is added after Servlet 2.4 for request operations. It mainly uses the ServletRequestListener and ServletRequestAttributeListener interfaces. Request status listener: The ServletRequestListener interface can be used to listen to each user request. servlet. servletRequestListener interface, which is defined as follows: public void requestInitialized (ServletRequestEvent sre) public void requestDestroyed (inclusre, the operation method defined by this event class is as follows: public ServletRequest getServletRequest () public ServletContext getServletContext () listens to user request requests
Package com. oumyye. listener; import javax. servlet. servletRequestEvent; import javax. servlet. servletRequestListener; public class ServletRequestListenerDemo implements ServletRequestListener {public void requestInitialized (ServletRequestEvent event) {System. out. println ("** request initialization. Http: // "+ event. getServletRequest (). getRemoteAddr () + event. getServletContext (). getContextPath ();} public void requestDestroyed (ServletRequestEvent event) {System. out. println ("** request destruction. Http: // "+ event. getServletRequest (). getRemoteAddr () + event. getServletContext (). getContextPath ());}}

Web. xml configuration

<Listener> <listener-class> com. oumyye. listener. ServletRequestListenerDemo </listener-class> </listener>
Request attribute listening: The ServletRequestAttributeListener interface can use javax to listen to the request range attribute. servlet. servletRequestAttributeListener buteadded (ServletRequestAttributeEvent srae) public void attributeReplaced (ServletRequestAttributeEvent srae) public void attributeRemoved (ServletRequestAttributeEvent srae) after the listener is added, the request attribute operation will generate a ServletRequestAttributeEvent. The event is defined as follows: public String getName () public Object getValue () listens to the request attribute operation.
Package com. oumyye. listener; import javax. servlet. servletRequestAttributeEvent; import javax. servlet. servletRequestAttributeListener; public class ServletRequestAttributeListenerDemo implements ServletRequestAttributeListener {public void attributeAdded (ServletRequestAttributeEvent) {System. out. println ("** add request property --> property name:" + event. getName () + ", attribute content:" + event. getValue ();} public void attributeRemoved (ServletRequestAttributeEvent event) {System. out. println ("** Delete request Attributes --> attribute name:" + event. getName () + ", attribute content:" + event. getValue ();} public void attributeReplaced (ServletRequestAttributeEvent event) {System. out. println ("** Replace the request property --> property name:" + event. getName () + ", attribute content:" + event. getValue ());}}

Web. xml configuration

<Listener> <listener-class> com. oumyye. listener. ServletRequestAttributeListenerDemo </listener-class> </listener>
Listener instance-Online Personnel Statistics Online personnel list is a common function. After a user logs in successfully, the name of this user is added to the list, in this way, you can know the users that are currently online. This function can only be implemented by listeners on the WEB. To use an interface to complete the listener of the online user list, you need to use the following interfaces: ServletContextListener interface: Set an empty set to the application during context initialization; HttpSessionAttributeListener interface: when a user adds the session attribute, it indicates that the new user logs in. the user's login name is retrieved from the sesion, and then the user is saved in the list. HttpSessionListener interface: when a user logs out (manual logout or session timeout), the user is deleted from the user list. Create LoginList in the code process. java to store the package com. oumyye. listener; import java. util. *; public class LoginList {private static LoginList user = new LoginList (); private Vector vector = null; // private calls the constructor, // prevent new instance object public LoginList () {this. vector = new Vector () ;}// the instance Object in external use is public static LoginList getInstance () {return user ;}// the user logs on to public boolean addLoginList (String user) {if (User! = Null) {this. vector. add (user); return true;} else {return false ;}// gets the user list public Vector getList () {return vector ;} // Delete the public void removeLoginList (String user) {if (user! = Null) {vector. removeElement (user );}}}LoginList. java

Create a LoginNote. java class to implement the HttpSessionBindingListener class

Package com. oumyye. listener; import javax. servlet. http. httpSessionBindingEvent; public class LoginNote implements javax. servlet. http. httpSessionBindingListener {private String user; private LoginList container = LoginList. getInstance (); public LoginNote () {user = "";} public void setUser (String user) {this. user = user;} public String getUser () {return this. user;} public void valueBound (HttpSessionB IndingEvent arg0) {System. out. println (this. user + "this user has been online");} public void valueUnbound (HttpSessionBindingEvent arg0) {System. out. println (this. user + "this user has been deprecated"); if (user! = "") {Container. removeLoginList (user );}}}LoginNote. java

Page files

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. * "errorPage =" "%>  

 

 

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.