ServletRequestListener的用法和实例

2021-08-02 17:54:14 浏览数 (1)

5)ServletRequestListener:

ServletRequestListener能监听到Request的生成和销毁。

例 2.2.5.1

package com;

import javax.servlet.ServletRequestEvent;

import javax.servlet.ServletRequestListener;

public class MyServletRequestListener implements

ServletRequestListener {

/*public void requestDestroyed(ServletRequestEvent sre)Notification that the servlet request is about to go out of scope. */

public void requestDestroyed(ServletRequestEvent arg0) {

System.out.println("马克-to-win destroyed" arg0.getServletRequest().getRemoteAddr());

}

/*public void requestInitialized(ServletRequestEvent sre)Notification that the servlet request is about to go into scope. */

public void requestInitialized(ServletRequestEvent arg0) {

System.out.println("马克-to-win request Initialized" arg0.getServletRequest().getRemoteAddr());

}

}

web.xml加入下面这段话:

<listener>

<listener-class>com.MyServletRequestListener</listener-class>

</listener>

用下面这两个servlet测试:

package com;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ServletHello1 extends HttpServlet {

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

System.out.println("ServletHello1 before");

request.getRequestDispatcher("/ServletHello2").forward(request,response);

System.out.println("这里会block5秒,ServletHello1 after");

}

}

更多请看:https://blog.csdn.net/qq_44638460/article/details/104107135

jsp

0 人点赞