Kela's Blog

            前面的路很坎坷,但毕竟是条路.也许走过这一段就会发现,走过去就是梦想中的地方.因此坚持成为此刻唯一能做且必须去做的事情.
posts - 9, comments - 27, trackbacks - 0, articles - 15
说明:A 表1为Servlet Life-Cycle Events,内容摘抄自“ The JavaTrademarked EE 5 Tutorial”的“Chapter 3: Java Servlet Technology
B WebContextListener.java SessionListenter.java RequestListenter.java分别为实现例子
C web.xml 为配置文件片段

表1 Servlet Life-Cycle Events
Object
Event
Listener Interface and Event Class
Web context
(see Accessing the Web Context)
Initialization and destruction
Attribute added, removed, or replaced
Creation, invalidation, activation, passivation, and timeout
Attribute added, removed, or replaced
Request
A servlet request has started being processed by web components
Attribute added, removed, or replaced

-- WebContextListener.java

public class WebContextListener implements ServletContextListener, ServletContextAttributeListener {
/*
* ServletContextListener and servletContextEvent
* Web context Destruction
*/
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("[INFO]:Web context 销毁... ...");
}

/*
* ServletContextListener and servletContextEvent
* Web context Initialization
*/
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("[INFO]:Web context 初始化 ... ..." );
}

/*
* ServletContextAttributeListener and ServletContextAttributeEvent
* Web context Attribute Added
*
* ServletContext servletContext = this.getServletContext();
* - Added
* servletContext.setAttribute("name", "Kela");
* - Replaced
* servletContext.setAttribute("name", "KANGFENG");
* - Removed
* servletContext.removeAttribute("name");
*
*/
public void attributeAdded(ServletContextAttributeEvent arg0) {
System.out.println("[INFO]:Web context's attribute added");
System.out.println("[INFO]:name is " + arg0.getServletContext().getAttribute("name"));
}

public void attributeRemoved(ServletContextAttributeEvent arg0) {
System.out.println("[INFO]:Web context's attribute removed");
System.out.println("[INFO]:name is " + arg0.getServletContext().getAttribute("name"));
}

public void attributeReplaced(ServletContextAttributeEvent arg0) {
System.out.println("[INFO]:Web context's attribute replaced");
System.out.println("[INFO]:name is " + arg0.getServletContext().getAttribute("name"));
}
}

-- SessionListenter.java
public class SessionListenter implements HttpSessionListener, HttpSessionActivationListener, HttpSessionAttributeListener {

/*
* 创建Session
* HttpSession session = request.getSession();
* 新增Attribute
* session.setAttribute("userName", "kela");
* 修改Attribute
* session.setAttribute("userName", "KangFeng");
* 删除Attribute
* session.removeAttribute("userName");
* 销毁Session
* session.invalidate();
*/
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("[INFO]: HttpSession 对象被创建... ...");

}

public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("[INFO]: HttpSession 对象被摧毁... ...");
}

public void sessionDidActivate(HttpSessionEvent arg0) {
System.out.println("::::::::::::");
}

public void sessionWillPassivate(HttpSessionEvent arg0) {
System.out.println("::::::::::::");
}

public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("[INFO]: 新增 HttpSession's attribute");
System.out.println("[INFO]: userName = " + arg0.getSession().getAttribute("userName"));
}

public void attributeRemoved(HttpSessionBindingEvent arg0) {
System.out.println("[INFO]: 移除 HttpSession's attribute");
System.out.println("[INFO]: userName = " + arg0.getSession().getAttribute("userName"));

}

public void attributeReplaced(HttpSessionBindingEvent arg0) {
System.out.println("[INFO]: 修改 HttpSession's attribute");
System.out.println("[INFO]: userName = " + arg0.getSession().getAttribute("userName"));
}
}

-- RequestListenter.java
public class RequestListenter implements ServletRequestListener, ServletRequestAttributeListener {

public void requestDestroyed(ServletRequestEvent arg0) {
System.out.println("[INFO]: A servlet request 对象被销毁... ...");
}

public void requestInitialized(ServletRequestEvent arg0) {
System.out.println("[INFO]: A servlet request 对象被创建... ...");
}

/*
* 新增
* request.setAttribute("requUserName", "Kela");
* 修改
* request.setAttribute("requUserName", "KangFeng");
* 移除
* request.removeAttribute("requUserName");
*/
public void attributeAdded(ServletRequestAttributeEvent arg0) {
System.out.println("[INFO]: A servlet request'attribute 新增... ...");
System.out.println("[INFO]: requUserName = " + arg0.getServletRequest().getAttribute("requUserName"));
}

public void attributeRemoved(ServletRequestAttributeEvent arg0) {
System.out.println("[INFO]: A servlet request'attribute 移除... ...");
System.out.println("[INFO]: requUserName = " + arg0.getServletRequest().getAttribute("requUserName"));
}

public void attributeReplaced(ServletRequestAttributeEvent arg0) {
System.out.println("[INFO]: A servlet request'attribute 修改... ...");
System.out.println("[INFO]: requUserName = " + arg0.getServletRequest().getAttribute("requUserName"));
}
}

-- web.xml
<listener>
    <listener-class>com.kela.web.listeners.WebContextListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.kela.web.listeners.SessionListenter</listener-class>
  </listener>
  <listener>
    <listener-class>com.kela.web.listeners.RequestListenter</listener-class>
  </listener>

只有注册用户登录后才能发表评论。


网站导航:
 
分享到: