Jsp & Servlet 会话控制

作为J2EE的重要组成部分的jsp和servlet规范中,会话(session)处理占有非常重要的位置。目前,很多资料都非常详细的讲解了会话跟踪如何处理。但是,针对会话控制却很少有人涉及,原本在servlet规范中,servlet提供了HttpSessionContext接口处理会话控制功能,但是,在Servlet API 2.1以后,这个功能被取消了,参考原文(As of Java(tm) Servlet API 2.1 for security reasons, with no replacement. This interface will be removed in a future version of this API.)。

在本文中,作者会给你介绍一种会话控制的方法,采用listener技术,实现HttpSessionContext的功能替换。很多开发人员都会在部分场合方便得使用到这个功能完成某些任务,例如:在线人员信息查看,在线人员控制等等功能。



分析
本文采用实例方式介绍会话控制功能。使用若干jsp页面,和一个java类完成整个功能演示。详见下表:

组件
功能

Com.guipei.listener. SessionListener
监听组件,完成HttpSessionContext的功能

index.jsp
实现用户登陆,创建新的session

logout.jsp
实现用户退出,用户自动删除session

display.jsp
显示用户登陆信息,在用户登陆后自动转入

session.jsp
列出当前所有的session

kill.jsp
杀掉指定的会话,使这个用户连接无效




实现
监听类com.guipei.listener.SessionListener 实现web application的监听功能,它实现了HttpSessionListener接口,可以监听sessionCreated(HttpSessionEvent se)和sessionDestroyed(HttpSessionEvent se) 方法,因此我们可以很容易的在session的创建和销毁事件过程中处理session的控制。

在此类中,我们创建一个静态实例变量Hashtable ht,采用Hashtable的一个好处是它是线程安全的集合类,无须我们再多做线程处理。采用这个collection类保存我们所要控制的session对象。在监听事件中容易的处理相关任务。

参看全部代码:



package com.guipei.listener;



import java.util.Hashtable;

import java.util.Iterator;



import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;



public class SessionListener implements HttpSessionListener {



// 集合对象,保存session 对象的引用

static Hashtable ht = new Hashtable();



// 实现HttpSessionListener接口,完成session创建事件控制

public void sessionCreated(HttpSessionEvent arg0) {

HttpSession session = arg0.getSession();

ht.put(session.getId(), session );

System.out.println("create session :" + session.getId() );

}



// 实现HttpSessionListener接口,完成session销毁事件控制

public void sessionDestroyed(HttpSessionEvent arg0) {

HttpSession session = arg0.getSession();

System.out.println("destory session :" + session.getId() );

ht.remove(session.getId());

}



// 返回全部session对象集合

static public Iterator getSet( ){

return ht.values().iterator();

}



// 依据session id返回指定的session对象

static public HttpSession getSession(String sessionId){

return (HttpSession)ht.get(sessionId);

}

}






页面index.jsp 处理用户登陆,创建新的会话的功能。在完成验证后,跳转到display.jsp页面上。

<%@ page contentType="text/html; charset=gb2312" %>

<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->

<%

String strName = null;

String strThing = null;

try {

strName = request.getParameter("name");

strThing = request.getParameter("thing");

if ((strName == null) || (strName.length()==0)){

throw new Exception("null strName");

}

if ((strThing == null) || (strThing.length()==0))

throw new Exception("null strThing");



// add session

session.setAttribute("name",strName);

session.setAttribute("thing",strThing);

response.sendRedirect("display.jsp");



} catch (Exception e) {

}

%>

<html>

<head>

<title>Welcome</title>

</head>

<body>

<center>Welcome</center>



<form method='post' >

<table align='center'>

<tr>

<td>name:</td>

<td> <input name='name' type='input'/> </td>

</tr>

<tr>

<td>thing:</td>

<td> <input name='thing' type='input'/> </td>

</tr>

<tr>

<td align='right'> </td>

<td align='right'>

<button type='submit'>submit</button>

<button type='reset'>reset</button>

</td>

</tr>

</table>

</form>

</body>

</html>






页面display.jsp用于用户登陆后的显示功能,如果用户没有进行过登陆请求,会自动转发到index.jsp页面,保证用户登陆。

<%@ page language="java" pageEncoding="GB2312" %>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>Lomboz JSP</title>

</head>

<body bgcolor="#FFFFFF">



<%



if (session.isNew()==true){

response.sendRedirect("index.jsp");

}

out.println("name: "+ session.getAttribute("name") + "<br>");

out.println("thing: "+ session.getAttribute("thing") + "<br>");

out.println("session id: " + session.getId() + "<br>");

out.println("create time: " + session.getCreationTime() );



%>



</body>

</html>




页面logout.jsp用于用户退出登陆,采用主动方式销毁session。

<%@ page language="java" pageEncoding="GB2312" %>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>Lomboz JSP</title>

</head>

<body bgcolor="#FFFFFF">

<%

if(session.isNew()!=true){

session.invalidate();

}

response.sendRedirect("index.jsp");

%>

</body>

</html>




页面session.jsp列出当前会话用户,并提供一个连接到kill.jsp,可以用作销毁指定的会话操作。

<%@ page language="java" pageEncoding="GB2312" %>

<%@ page import= 'com.guipei.listener.*,java.util.*'%>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>Lomboz JSP</title>

</head>

<body bgcolor="#FFFFFF">



List session object

<br>

<table border='1'>

<tr bgcolor='yellow'>

<td>session id</td>

<td>user name </td>

<td>what thing </td>

<td>create time </td>

<td>operate</td>

</tr>



<%

Iterator iterator = SessionListener.getSet();

while(iterator.hasNext()){

try{

HttpSession session1 = (HttpSession)iterator.next();

out.println("<tr>");

out.println("<td>" + session1.getId() + "</td>" );

out.println("<td>" + session1.getAttribute("name") + "</td>" );

out.println("<td>" + session1.getAttribute("thing") + "</td>" );

out.println("<td>" + session1.getCreationTime() + "</td>" );

out.println("<td> <a href='kill.jsp?sessionid=" + session1.getId() +

"'>kill </a> </td>" );



out.println("</tr>");

System.out.println("list " + session1.getId());

}catch(Exception ex){

ex.printStackTrace();

return;

}

}

%>

</table>

</body>

</html>




页面kill.jsp实现销毁指定会话的功能,接收一个session id参数,从我们保存的session对象集合中取得对应的session对象,调用invalidate方法,销毁对象。

<%@ page language="java" pageEncoding="GB2312" %>

<%@ page import="com.guipei.listener.*"%>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>Lomboz JSP</title>

</head>

<body bgcolor="#FFFFFF">

<%

// kill the session

try {

String strSid = request.getParameter("sessionid");

HttpSession session1 = SessionListener.getSession(strSid);

if (session1!=null){

session1.invalidate();

}

} catch (Exception e) {

e.printStackTrace();

}

response.sendRedirect("session.jsp");

%>



</body>

</html>






完成以上代码后,还需要在web.xml描述中添加以下元素。使得SessionListener类可以发挥监听功能。

<listener>

<listener-class>

com.guipei.listener.SessionListener

</listener-class>

</listener>



总结
作者不是很清楚,servlet规范为什么要取消HttpSessionContext接口,尽管它声明了取消的原因,但是我们仍然可以容易的通过HttpSessionListener接口实现这个功能。

希望本文可以提供一个新的方法,替换已经被servlet规范废除的HttpSessionContext接口。让我们可以方便的进行会话操作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值