标签:not oncreate 的区别 bind remove https exti 后缀 创建
Servlet的上下文监听,它主要实现监听ServletContext的创建和销毁。可以完成自己想要的初始化工作。主要包括下面两个方法
//启动服务的时候调用该方法
public void contextInitialized(ServletContextEvent sce);
//销毁该服务的时候调用该方法
public void contextDestroyed(ServletContextEvent sce);
监听ServletRequest域创建与销毁的监听器。ServletRequest的生命周期:每一次请求都会创建request,请求结束则销毁。主要包括下面两个方法
//访问服务器的任何资源都会调用该方法
public void requestDestroyed(ServletRequestEvent sre);
//当服务器对本次请求作出相应的时候,调用该方法
public void requestInitialized(ServletRequestEvent sre);
监听Httpsession域的创建与销毁的监听器。HttpSession对象的生命周期:第一次调用request.getSession时创建;销毁有以下几种情况(服务器关闭、session过期、 手动销毁)主要包括下面两个方法
//session创建的时候调用该方法
public void sessionCreated(HttpSessionEvent se);
//session销毁的时候调用该方法
public void sessionDestroyed(HttpSessionEvent se);
主要实现监听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);
主要包含下面三个方法
/**
* 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);
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);
还有下面两个监听器
接口实现监听HTTP会话中对象的绑定信息。它是唯一不需要在web.xml中设定Listener的
该接口实现监听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>
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();
chain.doFilter(request, response);// 放行, 让请求到达下一个目标。
全路径匹配 以 / 开始
/LoginServlet
以目录匹配 以 / 开始 以 * 结束
/demo01/*
以后缀名匹配 以 * 开始 以后缀名结束
.jsp .html *.do
标签:not oncreate 的区别 bind remove https exti 后缀 创建
原文地址:https://www.cnblogs.com/haizhilangzi/p/12318302.html