定义监听器
就是对内置对象的状态或者属性变化进行监听并且做出反应的特殊的servlet,可以在web,xml配置。
内置对象的状态:产生、销毁
属性:增加、删除、修改
package com.biubiu.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class RequestListener implements ServletRequestListener{
@Override
public void requestDestroyed(ServletRequestEvent event) {
System.out.println("销毁request内置对象时候触发");
}
@Override
public void requestInitialized(ServletRequestEvent event) {
System.out.println("产生了request内置对象的时候触发"+event.getServletContext());//取得服务器上下文关系
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID">
<display-name>MvcProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.biubiu.listener.RequestListener</listener-class>
</listener>
<servlet>
<servlet-name>empServlet</servlet-name>
<servlet-class>com.biubiu.controller.EmpServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>smith</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>empServlet</servlet-name>
<url-pattern>/emp/*</url-pattern>
</servlet-mapping>
</web-app>
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
使用request监听器观察客户端和服务器端转发
public class EmpServlet extends HttpServlet{
//取得业务层实现类对象
private IEmpService empService = (IEmpService)ServiceFactory.getInstance(EmpServicelmpl.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
//服务器端转化
req.getRequestDispatcher("/Pages/welcome.jsp").forward(req, resp);
//客户端转化
resp.sendRedirect("/MvcProject/Pages/welcome.jsp");
}
服务器端转发就是一个请求,那么整个过程只会创建一个request内置对象
客户端转发,会创建新的request内置对象,也就意味着发送了两次请求
监听request内置对象的属性变化
package com.biubiu.listener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
public class RequestAttributeListener implements ServletRequestAttributeListener{
@Override
public void attributeAdded(ServletRequestAttributeEvent arg0) {
System.out.println("增加一个属性触发"+arg0.getName()+":"+arg0.getValue());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent arg0) {
System.out.println("删除一个属性触发"+arg0.getName()+":"+arg0.getValue());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent arg0) {
System.out.println("替换一个属性触发"+arg0.getName()+":"+arg0.getValue());
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
//增加属性
req.setAttribute("name", "jack");
//替换属性
req.setAttribute("name", "tom");
//删除属性
req.removeAttribute("name");
}
修改配置文件
<listener>
<listener-class>com.biubiu.listener.RequestAttributeListener</listener-class>
</listener>