1. 程式人生 > >JSP/Servlet例項篇(線上使用者列表)

JSP/Servlet例項篇(線上使用者列表)

前言

好多沒有寫部落格了 , 因為將要寫的東西全當成筆記記錄起來了 , 所以就沒有寫部落格 , 以後部落格會記錄一些平時遇到的問題和一些想和大家分享的技術點和技巧和話語.希望可以和大家做朋友 , 一起為自己的目標而奮鬥 , 努力 , 努力 , 努力

知識點分類

這裡涉及到的知識點並不會進行太多的介紹 , 詳情大家可以自己去看書籍或視訊學習.並且做出的Web前端介面這裡並不美化, 主要學習這種方法.

  1. Servlet監聽器
  2. Java基礎知識
  3. Servlet各種物件(對應著JSP的各種內建物件)
  4. JSP的屬性範圍(瞭解即可)

成功圖

程式碼

Servlet監聽器

  • 路徑:src目錄下的 com.ghoset.listener.OnlineUserList
package com.ghoset.listener;

import java.util.Set;
import java.util.TreeSet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import
javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * Servlet監聽器例項 * @author Ghoset * */ public class OnlineUserList implements HttpSessionAttributeListener , HttpSessionListener , ServletContextListener { private
ServletContext app = null; //application物件 /** * 初始化操作 * 建立一個set集合 */ //初始化時建立一個例項物件 , 用來儲存使用者資訊 @Override public void contextInitialized(ServletContextEvent arg0) { this.app = arg0.getServletContext(); this.app.setAttribute("online" , new TreeSet() ); } /** * Session失效了 * 刪除這個session中的使用者資訊 */ @Override public void sessionDestroyed(HttpSessionEvent arg0) { Set all = (Set )this.app.getAttribute("online" ); all.remove(arg0.getSession().getAttribute("userName" ) );//獲取這個session中的使用者資訊 this.app.setAttribute("online" , all ); } /** * 使用者登陸了 * 往集合中新增一個使用者資訊 */ @Override public void attributeAdded(HttpSessionBindingEvent arg0) { Set all = (Set)this.app.getAttribute("online" ); all.add(arg0.getValue() ); this.app.setAttribute("online", all ); } /** * 使用者登出了 * 往集合中減少一個使用者資訊 */ @Override public void attributeRemoved(HttpSessionBindingEvent arg0) { Set all = (Set )this.app.getAttribute("online" ); all.remove(arg0.getValue() ); //往集合中刪除這個值 this.app.setAttribute("online" , all ); } /** * 以下方法沒有被使用到 , 如果有要求則可以新增 */ @Override public void attributeReplaced(HttpSessionBindingEvent arg0) { } @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void sessionCreated(HttpSessionEvent arg0) { } }

使用者登陸頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <section>
        <form method="post" action="listener/login.jsp" >
            <p>使用者名稱:<input type="text" name="userName" ></p>
            <p><input type="submit" value="登陸"></p>
        </form>
    </section>

    <%//驗證使用者請求資訊
        request.setCharacterEncoding("utf-8" );
        String name = request.getParameter("userName" );
        if(null != name && name.length() > 0 ){
            session.setAttribute(name , name );         //注意我們設定的session名字不能一樣 , 不然監聽器獲取不到新建立session
            response.sendRedirect("list.jsp" );
        } else{
            //out.println("<script>alert('使用者名稱輸入用誤!')</script>" );
            //response.sendRedirect("login.jsp" );
        }
     %>
  </body>
</html>

線上使用者列表

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.text.*" %>
<h2>線上使用者列表資訊如下</h2>
<table border="1px" >
    <tr>
        <th>使用者名稱稱</th>
        <th>更新時間</th>
    </tr>
<%//遍歷出所有的線上使用者列表
    Set all = (Set) application.getAttribute("online" );
    Iterator iterator = all.iterator();
    while(iterator.hasNext() ){
%>
    <tr>
        <td><%=iterator.next()  %></td>
        <td><%=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss" ).format(new Date() ) %></td>
    </tr>

<%  
    }
%>

web.xml

  <listener>
    <listener-class>com.ghoset.listener.OnlineUserList</listener-class>
  </listener>