The following class implements two interfaces, so that this class can have multiple functions (listening for changes to attributes in the request and request range)
1. Compile our class
The code is as follows: |
Copy code |
Package org. Rudiment. Listener; Import javax. servlet. ServletRequestAttributeEvent; Import javax. servlet. ServletRequestAttributeListener; Import javax. servlet. ServletRequestEvent; Import javax. servlet. ServletRequestListener; Import javax. servlet. http. HttpServletRequest; Public class MyRequestListener implements ServletRequestListener, ServletRequestAttributeListener { @ Override Public void attributeAdded (ServletRequestAttributeEvent arg0 ){ System. out. println ("attribute in the Request range:" + arg0.getName () + "added, value:" + arg0.getValue ()); } @ Override Public void attributeRemoved (ServletRequestAttributeEvent arg0 ){ System. out. println ("attribute in the Request range:" + arg0.getName () + "deleted, value:" + arg0.getValue ()); } @ Override Public void attributeReplaced (ServletRequestAttributeEvent arg0 ){ System. out. println ("attribute in the Request range:" + arg0.getName () + "value updated to:" + arg0.getValue ()); } @ Override Public void requestDestroyed (ServletRequestEvent arg0 ){ HttpServletRequest request = (HttpServletRequest) arg0.getServletRequest (); System. out. println ("The request is about to end "); } @ Override Public void requestInitialized (ServletRequestEvent arg0 ){ HttpServletRequest request = (HttpServletRequest) arg0.getServletRequest (); System. out. println ("The request is about to begin. The request comes from" + request. getHeader ("Referer") + "send" + request. getRequestURI ()); } } |
2. Configure our listener in web. xml
The code is as follows: |
Copy code |
<Listener> <Listener-class> org. Rudiment. Listener. MyRequestListener </listener-class> </Listener> |