JavaWeb监听器
ServletContextListener监听器
Servlet的上下文监听,它主要实现监听ServletContext的创建和销毁。可以完成自己想要的初始化工作。主要包括下面两个方法
//启动服务的时候调用该方法
public void contextInitialized(ServletContextEvent sce);
//销毁该服务的时候调用该方法
public void contextDestroyed(ServletContextEvent sce);
ServletRequestListener监听器
监听ServletRequest域创建与销毁的监听器。ServletRequest的生命周期:每一次请求都会创建request,请求结束则销毁。主要包括下面两个方法
//访问服务器的任何资源都会调用该方法
public void requestDestroyed(ServletRequestEvent sre);
//当服务器对本次请求作出相应的时候,调用该方法
public void requestInitialized(ServletRequestEvent sre);
HttpSessionListener监听器
监听Httpsession域的创建与销毁的监听器。HttpSession对象的生命周期:第一次调用request.getSession时创建;销毁有以下几种情况(服务器关闭、session过期、 手动销毁)主要包括下面两个方法
//session创建的时候调用该方法
public void sessionCreated(HttpSessionEvent se);
//session销毁的时候调用该方法
public void sessionDestroyed(HttpSessionEvent se);
ServletContextAttributeListener监听器
主要实现监听ServletContext属性的增加,删除和修改。主要包括下面三个方法
/**
* Receives notification that an attribute has been added to the
* ServletContext.
*
* @param event the ServletContextAttributeEvent containing the
* ServletContext to which the attribute was added, along with the
* attribute name and value
*/
public void attributeAdded(ServletContextAttributeEvent event);
/**
* Receives notification that an attribute has been removed
* from the ServletContext.
*
* @param event the ServletContextAttributeEvent containing the
* ServletContext from which the attribute was removed, along with
* the attribute name and value
*/
public void attributeRemoved(ServletContextAttributeEvent event);
/*
* Receives notification that an attribute has been replaced
* in the ServletContext.
*
* @param event the ServletContextAttributeEvent containing the
* ServletContext in which the attribute was replaced, along with
* the attribute name and its old value
*/
public void attributeReplaced(ServletContextAttributeEvent event);
ServletRequestAttributeListener监听器
主要包含下面三个方法
/**
* Receives notification that an attribute has been added to the
* ServletRequest.
*
* @param srae the ServletRequestAttributeEvent containing the
* ServletRequest and the name and value of the attribute that was
* added
*/
public void attributeAdded(ServletRequestAttributeEvent srae);
/**
* Receives notification that an attribute has been removed from the
* ServletRequest.
*
* @param srae the ServletRequestAttributeEvent containing the
* ServletRequest and the name and value of the attribute that was
* removed
*/
public void attributeRemoved(ServletRequestAttributeEvent srae);
/**
* Receives notification that an attribute has been replaced on the
* ServletRequest.
*
* @param srae the ServletRequestAttributeEvent containing the
* ServletRequest and the name and (old) value of the attribute
* that was replaced
*/
public void attributeReplaced(ServletRequestAttributeEvent srae);
HttpSessionListener监听器
HTTP会话监听,该接口实现监听HTTP会话创建、销毁。主要包括下面两个方法
/**
* Receives notification that a session has been created.
*
* @param se the HttpSessionEvent containing the session
*/
public void sessionCreated(HttpSessionEvent se);
/**
* Receives notification that a session is about to be invalidated.
*
* @param se the HttpSessionEvent containing the session
*/
public void sessionDestroyed(HttpSessionEvent se);
还有下面两个监听器
HttpBindingListener监听器
接口实现监听HTTP会话中对象的绑定信息。它是唯一不需要在web.xml中设定Listener的
HttpSessionAttributeListener监听器
该接口实现监听HTTP会话中属性的设置请求
可以通过下面的代码来查看前三个监听器的区别
MyHttpSessionListener
public class MyHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("MyHttpSessionListener.sessionCreated 的方法啦");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("MyHttpSessionListener.sessionDestroyed 的方法啦");
}
}
MyServletContextListerner
public class MyServletContextListerner implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//比如可以在这里实现数据库连接的初始化等操作
System.out.println("MyServletContextListerner.contextInitialized 方法被调用啦");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("MyServletContextListerner.contextDestroyed 方法被调用啦");
}
}
MyservletRequestListner
public class MyServletRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("MyServletRequestListener.requestDestroyed 方法被调用啦");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("MyServletRequestListener.requestInitialized 方法被调用啦");
}
}
在web中配置监听器
<listener>
<listener-class>com.rookie.bigdata.Listener.MyServletContextListerner</listener-class>
<listener-class>com.rookie.bigdata.Listener.MyServletRequestListener</listener-class>
<listener-class>com.rookie.bigdata.Listener.MyHttpSessionListener</listener-class>
</listener>
javaWeb Filter
filter与servlet在很多的方面极其相似,但是也有不同,例如filter和servlet一样都又三个生命周期方法,同时他们在web.xml中的配置文件也是差不多的、 但是servlet主要负责处理请求,而filter主要负责拦截请求和放行。
FIlter接口有三个方法
//服务器启动的时候执行该方法
public void init(FilterConfig filterConfig) throws ServletException;
//执行过滤
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException;
//服务器停止的时候执行该方法
public void destroy();
Filter的执行顺序
- 客户端发出请求,先经过过滤器, 如果过滤器放行,那么才能到servlet
- 如果有多个过滤器, 那么他们会按照注册的映射顺序 来 排队。 只要有一个过滤器, 不放行,那么后面排队的过滤器以及咱们的servlet都不会收到请求。
- init方法的参数 FilterConfig , 可以用于获取filter在注册的名字 以及初始化参数。 其实这里的设计的初衷与ServletConfig是一样的。
- 如果想放行,那么在doFilter 方法里面操作,使用参数 chain
chain.doFilter(request, response);// 放行, 让请求到达下一个目标。
- /* 写法格式与servlet一样。
- 全路径匹配 以 / 开始
/LoginServlet
- 以目录匹配 以 / 开始 以 * 结束
/demo01/*
- 以后缀名匹配 以 * 开始 以后缀名结束
.jsp .html *.do
原文地址:https://www.cnblogs.com/haizhilangzi/p/12318302.html