Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 初始Servlet監聽器

初始Servlet監聽器

編輯:關於Android編程

Servlet監聽器簡介
Servlet監聽器的作用是監聽Web容器的有效事件,由容器管理。利用Listener接口監聽在容器中的某個執行程序,並更具應用程序的需求做出適當的響應。


監聽Servlet上下文
Servlet上下文監聽可以監聽ServletContext對象的創建、刪除和添加屬性,以及刪除和修改操作,該監聽器需要用到下面兩個接口。

ServletContextListener接口:監聽ServletContext的創建和刪除。
class myServletContextListener implements ServletContextListener{

    //通知正在收聽的對象應用程序已經被加載及初始化
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

    }

    //通知正在收聽的對象應用程序已經被載出,即關閉
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
ServletAttributeListener接口:監聽ServletContext屬性的修改、刪除和增加
class myServletAttributeListener implements ServletContextAttributeListener{
    //若有對象加入Application范圍,通知正在收聽的對象
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {

    }
    //若有對象從Application范圍移除,通知正在收聽的對象
    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {

    }
    //若有對象在Application范圍內的一個對象被另一個對象替換,通知正在收聽的對象
    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {

    }
}

class myServletAttributeListener2 implements ServletRequestAttributeListener{

    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
}

監聽HTTP會話

HttpSessionListener接口:監聽HTTP會話的創建及銷毀

class myHttpSessionListener implements HttpSessionListener{

    //通知正在收聽的對象,session已經被加載及初始化
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {

    }
    //通知正在收聽的對象,session被載出
    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

    }
}
HttpSessionActivationListener接口:監聽HTTP會話active和passivate情況
class myHttpSessionActivationListener implements HttpSessionActivationListener{
    //通知正在收聽的對象,其session狀態變為有效狀態
    @Override
    public void sessionWillPassivate(HttpSessionEvent httpSessionEvent) {

    }
    //通知正在收聽的對象,其session狀態變為無效狀態
    @Override
    public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {

    }
}
HttpSessionBindingListener接口:監聽HTTP會話中對象的綁定信息,它是唯一不需要再web.xml中設置Listener的。
class myHttpBindingListener implements HttpSessionBindingListener{
    //當有對象加入session的范圍時,會被自動調用
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
    //當有對象從session的范圍移除時,會被自動取消調用
    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}
HttpSessionAttributeListener接口:監聽HTTP會話中屬性的設置請求。
class myHttpSessionAttributeListener implements HttpSessionAttributeListener {

    //若有對象加入Session范圍,通知正在收聽的對象
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    //若有對象從Session范圍移除,通知正在收聽的對象
    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    //若有對象在Session范圍內的一個對象被另一個對象替換,通知正在收聽的對象
    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }
}

監聽Servlet請求

ServletRequestListener接口:
class myServletRequestListener implements ServletRequestListener{

    //通知正在收聽的對象,ServletRequest已經被加載及初始化
    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent) {

    }
    //通知正在收聽的對象,ServletRequest已經被載出,即關閉
    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent) {

    }
}
ServletRequestAttributeListener接口:
class myServletRequestAttributeListener implements ServletRequestAttributeListener{
    //若有對象加入Request范圍,通知正在收聽的對象
    @Override
    public void attributeAdded(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
    //若有對象從Request范圍移除,通知正在收聽的對象
    @Override
    public void attributeRemoved(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
    //若有對象在Request范圍內的一個對象被另一個對象替換,通知正在收聽的對象
    @Override
    public void attributeReplaced(ServletRequestAttributeEvent servletRequestAttributeEvent) {

    }
}

下面使用一個小例子:使用監聽器查看在線用戶
UserInfoList類:

package com.java.model;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Created by YEN on 2016/7/10 and 22:05.
 */
public class UserInfoList {
    private static UserInfoList userInfoList=new UserInfoList();
    private ArrayList arrayList=null;
    public UserInfoList(){
        this.arrayList=new ArrayList();
    }
    public static UserInfoList getInstance(){
        return userInfoList;
    }
    //增加用戶
    public boolean addUserInfo(String username){
        if(username!=null){
            this.arrayList.add(username);
            return true;
        }else return false;
    }
    //獲取用戶列表
    public ArrayList getUserList(){
        return arrayList;
    }
    //移除用戶
    public void removeUserInfo(String username){
        if(username!=null){
            arrayList.remove(username);
        }
    }
}

UserInfoTrace類:

package com.java.model;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * Created with IntelliJ IDEA.
 * Created by YEN on 2016/7/10 and 22:12.
 */

public class UserInfoTrace implements HttpSessionBindingListener{
    private String username;
    private UserInfoList container=UserInfoList.getInstance();
    public UserInfoTrace(){
        this.username="";
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("上線................"+this.username);
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        System.out.println("下線................"+this.username);
        if(username!=""){
            container.removeUserInfo(username);
        }
    }
}

login.jsp頁面

<%--
  Created by IntelliJ IDEA.
  User: YEN
  Date: 2016/7/9
  Time: 16:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        function checkForm() {
            if(document.getElementById("username").value==null || document.getElementById("username").value==""){
                  alert("用戶名不能為空!");
                  return false;
              }
            if(document.getElementById("password").value==null || document.getElementById("password").value==""){
                alert("密碼不能為空!");
                return false;
            }
            return true;
        }
    </script>
</head>
<body>
<form name="form1" action="<%=request.getContextPath()%>/showUserInfo.jsp" method="get" onsubmit="return checkForm()">
    username:<input type="text" name="username" id="username">   <br>
    password:<input type="password" name="password" id="password">   <br>
    <input type="submit" value="提交">
</form>
</body>
</html>

showUserInfo.jsp頁面

<%--
  Created by IntelliJ IDEA.
  User: YEN
  Date: 2016/7/10
  Time: 22:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="com.java.model.UserInfoList" language="java" %>
<%@page import="com.java.model.UserInfoTrace" language="java" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
    <title>UserInfoPage</title>
</head>
<body>
<%
    UserInfoList userInfoList=UserInfoList.getInstance();  //獲得UserInfoList類的對象
    UserInfoTrace userInfoTrace=new UserInfoTrace();       //創建UserInfoTrace類的對象
    request.setCharacterEncoding("UTF-8");
    String username=request.getParameter("username");
    userInfoTrace.setUsername(username);
    session.setAttribute("list",userInfoTrace);
    userInfoList.addUserInfo(username);
    session.setMaxInactiveInterval(30);                   //session過期時間為30s
%>
<textarea rows="8" cols="20">
    <%
        ArrayList arrayList=userInfoList.getUserList();
        if(arrayList!=null && arrayList.size()>0 ){
            for (int i = 0; i < arrayList.size(); i++) {
                out.print(arrayList.get(i));
            }
        }
    %>
</textarea>
</body>
</html>

運行結果:

一:在login.jsp頁面輸入信息
這裡寫圖片描述
二:點擊登陸按鈕
這裡寫圖片描述
這裡寫圖片描述

三:在登陸一個用戶
這裡寫圖片描述

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved