小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

web.xml中l(wèi)istener作用及使用

 Erica_Shea 2015-01-05

一.WebContextLoaderListener 監(jiān)聽(tīng)類(lèi)
它能捕捉到服務(wù)器的啟動(dòng)和停止,在啟動(dòng)和停止觸發(fā)里面的方法做相應(yīng)的操作!
它必須在web.xml 中配置才能使用,是配置監(jiān)聽(tīng)類(lèi)的

二.下面是搜集的一些listener方面的知識(shí)
簡(jiǎn)例一
監(jiān)聽(tīng)用戶上線與退出,顯示在線用戶

1、登陸頁(yè)面 Login.jsp

<%@page pageEncoding="gb2312" contentType="text/html; charset=gb2312" %>
<%
session=request.getSession(false);
if(session!=null)session.invalidate();
%>
<html>
<head><title></title></head>
<body>
<form action="isOnline.jsp" method="post">
用戶名:<input type="text" name="uName"/>
<input type="submit" value="上線">
</form>
</body>
</html>

2、控制頁(yè)面(只是為了說(shuō)明監(jiān)聽(tīng)器問(wèn)題,所以簡(jiǎn)單了點(diǎn)...) isOnline.jsp

<%@page pageEncoding="gb2312" contentType="text/html; charset=gb2312" %>
<html>
<head><title></title></head>
<body>
<%
session=request.getSession();
session.setAttribute("userName",request.getParameter("uName"));
response.sendRedirect("showOnline.jsp");
%>
</body>
</html>


3、顯示頁(yè)面 showOnline.jsp

<%@page pageEncoding="gb2312" contentType="text/html; charset=gb2312" import="java.util.ArrayList" %>
<html>
<head><title></title></head>
<body>
<%
ArrayList showList=(ArrayList)(getServletContext().getAttribute("list"));
out.print("在線人數(shù) "+showList.size()+"<br>");
for(int i=0;i<showList.size();i++){
out.print(showList.get(i)+"在線"+"<br>");
}
%>
<br>
<a href="Login.jsp">退出</a>
</body>
</html>

4、配置頁(yè)面 web.xml

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java./dtd/web-app_2_3.dtd">
<web-app>
<listener>
    <listener-class>org.xiosu.listener.onlineListener</listener-class>
</listener>
</web-app>

5、監(jiān)聽(tīng)器 onlineListener.java

package org.xiosu.listener;

import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class onlineListener implements HttpSessionListener,
HttpSessionAttributeListener {
// 參數(shù)
ServletContext sc;
ArrayList list = new ArrayList();
// 新建一個(gè)session時(shí)觸發(fā)此操作
public void sessionCreated(HttpSessionEvent se) {
sc=se.getSession().getServletContext();
System.out.println("新建一個(gè)session");
}
// 銷(xiāo)毀一個(gè)session時(shí)觸發(fā)此操作
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("銷(xiāo)毀一個(gè)session");
if (!list.isEmpty()) {
   list.remove((String) se.getSession().getAttribute("userName"));
   sc.setAttribute("list", list);
}
}
// 在session中添加對(duì)象時(shí)觸發(fā)此操作,在list中添加一個(gè)對(duì)象
public void attributeAdded(HttpSessionBindingEvent sbe) {
list.add((String) sbe.getValue());
sc.setAttribute("list", list);
}
// 修改、刪除session中添加對(duì)象時(shí)觸發(fā)此操作
public void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
}
}


說(shuō)明:本例只為簡(jiǎn)單介紹監(jiān)聽(tīng)器,并未進(jìn)行安全方面設(shè)置。

監(jiān)聽(tīng)器也叫Listener,是Servlet的監(jiān)聽(tīng)器,它可以監(jiān)聽(tīng)客戶端的請(qǐng)求、服務(wù)端的操作等。通過(guò)監(jiān)聽(tīng)器,可以自動(dòng)激發(fā)一些操作,比如監(jiān)聽(tīng)在線的用 戶的數(shù)量。當(dāng)增加一個(gè)HttpSession時(shí),就激發(fā)sessionCreated(HttpSessionEvent   se)方法,這樣
就可以給在線人數(shù)加1。常用的監(jiān)聽(tīng)接口有以下幾個(gè):
ServletContextAttributeListener監(jiān)聽(tīng)對(duì)ServletContext屬性的操作,比如增加、刪除、修改屬性。
ServletContextListener監(jiān)聽(tīng)ServletContext。當(dāng)創(chuàng)建ServletContext時(shí),激發(fā) contextInitialized(ServletContextEvent   sce)方法;當(dāng)銷(xiāo)毀ServletContext時(shí),激發(fā)contextDestroyed(ServletContextEvent   sce)方法。
HttpSessionListener監(jiān)聽(tīng)HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session   Created(HttpSessionEvent   se)方法;當(dāng)銷(xiāo)毀一個(gè)Session時(shí),激發(fā)sessionDestroyed   (HttpSessionEvent   se)方法。
HttpSessionAttributeListener監(jiān)聽(tīng)HttpSession中的屬性的操作。當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā) attributeAdded(HttpSessionBindingEvent   se)   方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent   se)方法;當(dāng)在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent   se)   方法。
example:隨服務(wù)器啟動(dòng)
web-app>

      com.tb.listener.CountStartListener


package com.tb.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import com.tb.timertask.DoCountTask;
public class CountStartListener extends HttpServlet implements ServletContextListener
{
private static final long serialVersionUID = 1824920962239905170L;
public CountStartListener()
{
   // TODO Auto-generated constructor stub
}
public void contextDestroyed(ServletContextEvent arg0)
{
   // TODO Auto-generated method stub
}
   public void contextInitialized(ServletContextEvent arg0)
  
   DoCountTask.dotask();
}
}


概述:

Servlet監(jiān)聽(tīng)器用于監(jiān)聽(tīng)一些重要事件的發(fā)生,監(jiān)聽(tīng)器對(duì)象可以在事情發(fā)生前、發(fā)生后可以做一些必要的處理。

接口:

目前Servlet2.4和JSP2.0總共有8個(gè)監(jiān)聽(tīng)器接口和6個(gè)Event類(lèi),其中HttpSessionAttributeListener與

HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener則都使用HttpSessionEvent;其余Listener對(duì)應(yīng)的Event如下所示:

 

Listener接口

Event類(lèi)

ServletContextListener

ServletContextEvent

ServletContextAttributeListener

ServletContextAttributeEvent

HttpSessionListener

HttpSessionEvent

HttpSessionActivationListener

HttpSessionAttributeListener

HttpSessionBindingEvent

HttpSessionBindingListener

ServletRequestListener

ServletRequestEvent

ServletRequestAttributeListener

ServletRequestAttributeEvent

分別介紹:

一 ServletContext相關(guān)監(jiān)聽(tīng)接口

補(bǔ)充知識(shí):

通過(guò)ServletContext 的實(shí)例可以存取應(yīng)用程序的全局對(duì)象以及初始化階段的變量。

在JSP文件中,application 是 ServletContext 的實(shí)例,由JSP容器默認(rèn)創(chuàng)建。Servlet 中調(diào)用 getServletContext()方法得到 ServletContext 的實(shí)例。

注意:

全局對(duì)象即Application范圍對(duì)象,初始化階段的變量指在web.xml中,經(jīng)由<context-param>元素所設(shè)定的變量,它的范圍也是Application范圍,例如:

<context-param>

<param-name>Name</param-name>

<param-value>browser</param-value>

</context-param>

當(dāng)容器啟動(dòng)時(shí),會(huì)建立一個(gè)Application范圍的對(duì)象,若要在JSP網(wǎng)頁(yè)中取得此變量時(shí):

String name = (String)application.getInitParameter("Name");

或者使用EL時(shí):

${initPara.name}

若是在Servlet中,取得Name的值方法:

String name = (String)ServletContext.getInitParameter("Name");

1.ServletContextListener:

用于監(jiān)聽(tīng)WEB 應(yīng)用啟動(dòng)和銷(xiāo)毀的事件,監(jiān)聽(tīng)器類(lèi)需要實(shí)現(xiàn)javax.servlet.ServletContextListener 接口。

ServletContextListener 是 ServletContext 的監(jiān)聽(tīng)者,如果 ServletContext 發(fā)生變化,如服務(wù)器啟動(dòng)時(shí) ServletContext 被創(chuàng)建,服務(wù)器關(guān)閉時(shí) ServletContext 將要被銷(xiāo)毀。

ServletContextListener接口的方法:

void contextInitialized(ServletContextEvent sce)

通知正在接受的對(duì)象,應(yīng)用程序已經(jīng)被加載及初始化。

void contextDestroyed(ServletContextEvent sce)

通知正在接受的對(duì)象,應(yīng)用程序已經(jīng)被載出。

ServletContextEvent中的方法:

ServletContext getServletContext()

取得ServletContext對(duì)象

2.ServletContextAttributeListener:用于監(jiān)聽(tīng)WEB應(yīng)用屬性改變的事件,包括:增加屬性、刪除屬性、修改屬性,監(jiān)聽(tīng)器類(lèi)需要實(shí)現(xiàn)javax.servlet.ServletContextAttributeListener接口。

ServletContextAttributeListener接口方法:

void attributeAdded(ServletContextAttributeEvent scab)

若有對(duì)象加入Application的范圍,通知正在收聽(tīng)的對(duì)象

void attributeRemoved(ServletContextAttributeEvent scab)

若有對(duì)象從Application的范圍移除,通知正在收聽(tīng)的對(duì)象

void attributeReplaced(ServletContextAttributeEvent scab)

若在Application的范圍中,有對(duì)象取代另一個(gè)對(duì)象時(shí),通知正在收聽(tīng)的對(duì)象

ServletContextAttributeEvent中的方法:

java.lang.String getName()

回傳屬性的名稱(chēng)

java.lang.Object getValue()

回傳屬性的值

二、HttpSession相關(guān)監(jiān)聽(tīng)接口

1.HttpSessionBindingListener接口

注意:HttpSessionBindingListener接口是唯一不需要再web.xml中設(shè)定的Listener

當(dāng)我們的類(lèi)實(shí)現(xiàn)了HttpSessionBindingListener接口后,只要 對(duì)象加入 Session范圍(即調(diào)用HttpSession對(duì)象的setAttribute方法的時(shí)候)或從Session范圍中移出(即調(diào)用 HttpSession對(duì)象的 removeAttribute方法的時(shí)候或Session Time out的時(shí)候)時(shí),容器分別會(huì)自動(dòng)調(diào)用下列兩個(gè)方法:

void valueBound(HttpSessionBindingEvent event)

void valueUnbound(HttpSessionBindingEvent event)

思考:如何實(shí)現(xiàn)記錄網(wǎng)站的客戶登錄日志, 統(tǒng)計(jì)在線人數(shù)?

2.HttpSessionAttributeListener接口

HttpSessionAttributeListener監(jiān)聽(tīng)HttpSession中的屬性的操作。

當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā) attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。這和ServletContextAttributeListener比較類(lèi)似。

3.HttpSessionListener接口

HttpSessionListener監(jiān)聽(tīng) HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session Created(HttpSessionEvent se)方法;當(dāng)銷(xiāo)毀一個(gè)Session時(shí),激發(fā)sessionDestroyed (HttpSessionEvent se)方法。

4.HttpSessionActivationListener接口

主要用于同一個(gè)Session轉(zhuǎn)移至不同的JVM的情形。

四、ServletRequest監(jiān)聽(tīng)接口

1.ServletRequestListener接口

和ServletContextListener接口類(lèi)似的,這里由ServletContext改為ServletRequest

2.ServletRequestAttributeListener接口

和ServletContextListener接口類(lèi)似的,這里由ServletContext改為ServletRequest

有的listener可用于統(tǒng)計(jì)網(wǎng)站在線人數(shù)及訪問(wèn)量。 如下:

服務(wù)器啟動(dòng)時(shí)(實(shí)現(xiàn)ServletContextListener監(jiān)聽(tīng)器contextInitialized方法),讀取數(shù)據(jù)庫(kù),并將其用一個(gè)計(jì)數(shù)變量保存在application范圍內(nèi)

session創(chuàng)建時(shí)(實(shí)現(xiàn)HttpSessionListener監(jiān)聽(tīng)器sessionCreated方法),讀取計(jì)數(shù)變量加1并重新保存

服務(wù)器關(guān)閉時(shí)(實(shí)現(xiàn)ServletContextListener監(jiān)聽(tīng)器contextDestroyed方法),更新數(shù)據(jù)庫(kù)


轉(zhuǎn)自: http://www./wx886104/archive/2010/06/01/322419.html

 

      http://hht83.blog.163.com/blog/static/44037112008324232278/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多