A good memory is better than a bad pen. 41-javaweb application in ServletRequest domain monitoring (5)

Source: Internet
Author: User

The listener in Javaweb is a special class defined in the servlet specification that listens to the creation and destruction events of ServletContext, HttpSession, and ServletRequest domain objects in a Web application. and listening for events in which the properties in these domain objects have been modified.
Servletrequestlistener interface for monitoring the creation and destruction of ServletRequest objects
When the request object is created, the requestinitialized (Servletrequestevent sre) method is called
When the request object is destroyed, the requestdestroyed (Servletrequestevent sre) method is called
1. Write the source code to monitor the creation and destruction of ServletRequest domain objects
Implement Servletrequestlistener interface, monitor the creation and destruction of ServletRequest objects

 PackageCom.servlet.listener;ImportJavax.servlet.ServletRequestEvent;ImportJavax.servlet.ServletRequestListener;/** * Simple implementation of the Servletrequestlistener interface, can be ServletRequest object creation and destruction of these two actions to listen. * @author Fan Fangming * * Public  class Easyservletrequestlistenerimplements Servletrequestlistener {          @Override     Public void requestinitialized(Servletrequestevent SRE) {System.out.println ("-----------"+ sre.getservletrequest () +", ServletRequest create"); }@Override     Public void requestdestroyed(Servletrequestevent SRE) {System.out.println ("-----------"+ sre.getservletrequest () +", ServletRequest destroyed"); }}

2. Modify Web. xml

<listener>      <description>ServletRequestListener监听器</description>      <!--实现了ServletRequestListener接口的监听器类 -->      <listener-class>com.servlet.listener.EasyServletRequestListener</listener-class>  </listener>

3. index.jsp for testing

<%@ page language="java" import="java.util.*" pageencoding=  "UTF-8"%><! DOCTYPE html><html>  <head>    <title>My JSP ' index.jsp ' starting page</title>  </head>  <body>This is my JSP page.<br>To access the JSP page, the HttpSession is created with the following ID: ${pagecontext.session.id}</body></html>

4. Operation result
Start Web middleware, access from URL:
http://127.0.0.1:8080/webStudy/index.jsp
Console input Results:
——— –[email protected],servletrequest Create
--[email Protected],httpsession Object creation
——— –[email Protected],servletrequest destroyed

A good memory is better than a bad pen. 41-javaweb application in ServletRequest domain monitoring (5)

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.