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");
}
}
ServletRequestListener的用法和实例
最新推荐文章于 2024-01-08 15:32:39 发布