Java basics --- Servlet listener

Source: Internet
Author: User

Java basics --- Servlet listener
I will write down the specific application of the listener: The function is responsible for listening to various WEB operations. When related events are triggered, an event will be generated and processed, you can monitor applications, sessions, and requests 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> listens to sessions in the listener. It provides three interfaces for session listening: HttpSessionListener and HttpS. EssionAttributeListener and HttpSessionBindingListenersession status listener: The HttpSessionListener interface can implement javax 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) and public void sessionDestroyed (HttpSessionEvent se). After a session is created or destroyed, an HttpSessionEvent event is generated, the operation defined by this event is as follows: public HttpSession getSession () listens to the session package com. oumyye. listener; import javax. servlet. ht Tp. 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, the sessionCreated () event in the HttpSessionListener interface is triggered, but there are two different ways to trigger the sessionDestroyed () event during user destruction: Method 1: Call the invalidate () of the HttpSession Interface () method 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 will be triggered Based on the attribute operation, Each operation method generates an HttpSessionBindingEvent event. The operation is defined as follows: public HttpSession getSession (), public String getName (), and public Object getValue: package com. oumyye. listener; import javax. servlet. http. httpSessionAttributeListener; import javax. servlet. http. httpSessionBindingEvent; public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener {public void attributeAdded (HttpSessionBi NdingEvent 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 listener: The HttpSessionBindingListener interface can also listen on session attribute operations in the session listener, this is the same as listening for context attributes. To listen on session attribute operations, you can use javax. servlet. http. httpSessionAttributeLis Tener interface completed: 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 operation of the attribute. Each operation method will generate the HttpSessionBindingEvent event public HttpSession getSession () public String getName () public Object getValue () listening for package com. oumyye. listener; import javax. ser Vlet. 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 (HttpSe SsionBindingEvent 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: Same as session property listening: The HttpSessionBindingListener interface is in the W EB also provides a javax. 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 package com. ou Myye. 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 listener: The ServletRequestAttributeListener API can use javax to listen to request range attributes. servlet. servletRequestAttributeListener interface, which is defined as follows: public void attributeAdded (ServletRequestAttributeEvent srae) pub Lic void attributeReplaced (ipvsrae) public void attributeRemoved (ipvsrae) after being added to the listener, the request attribute operation will generate a ServletRequestAttributeEvent, which is defined as follows: public String getName () public Object getValue () listens to the request attribute to operate package com. oumyye. listener; import javax. servlet. servletRequestAttributeEvent; import javax. servlet. servletRequestAttributeListener; public class ServletReq UestAttributeListenerDemo implements ServletRequestAttributeListener {public void attributeAdded (ServletRequestAttributeEvent event) {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 ();} p Ublic 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, this user name will be added to the list so that you can know which users are currently online. This function can only be implemented by the listener 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.

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.