Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在APC中使用Session

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

1、使用Session

l         只要获得HttpSession对象,就可以对Session进行管理和使用

l         为了遵循MVC模式,对Session的使用应该放在Handler中实现

l         Handler中,可以使用DispatchContext这个上下文环境来获得HttpSession对象

l         下面是一个封装了简单使用Session的工具类:

package com.fujitsu.eFrame.eftool;
 
import javax.servlet.http.HttpSession;
 
import com.fujitsu.uji.DispatchContext;
import com.fujitsu.uji.http.HttpSessionProfile;
 
public class SessionUtil {
 
       private static HttpSession session;
       
       private static HttpSession getSession(DispatchContext context) {
         
              if (session == null) {
                session = ((HttpSessionProfile)context.getSessionProfile()).getSession();
              }
              return session;
         
       }
       
       public static Object getAttribute(DispatchContext context, String name) {
         
              return getSession(context).getAttribute(name);
         
       }
       
       public static void setAttribute(DispatchContext context, String name, Object value) {
         
         
              getSession(context).setAttribute(name, value);
         
       }
       
       public static void setTimeout(DispatchContext context, int seconds) {
         
              getSession(context).setMaxInactiveInterval(seconds);
         
         
       }
}

l         这里最主要的是使用APC提供的API获得HttpSession对象

Ø         DispatchContextgetSessionProfile方法获得HttpSessionProfile对象(要强制转换,因为SessionProfileHttpSessionProfile的抽象父类)

Ø         然后由HttpSessionProfilegetSession()方法获得HttpSession对象

l         另外要注意是setTimeout()方法中指定的Session超时的时间以秒为单位

l         这样就可以在Handler中使用Session,例如:

SessionUtil.setAttribute(context, "workArea", name);

2、监听Session

l         可以实现HttpSessionListener接口来实现监听Session的创建和销毁,下面是一个简单的框架:

package com.fujitsu.eFrame.eftool;
 
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
public class SessionListener implements HttpSessionListener {
 
       public void sessionCreated(HttpSessionEvent se) {
         
              System.out.println("Session created!");
         
       }
 
       public void sessionDestroyed(HttpSessionEvent se) {
         
              System.out.println("Session Destroyed!");
         
       }
 
}

l         HttpSessionListener接口提供了sessionCreated()sessionDestroyed()两个方法,分别在Session创建之后和Session销毁之前被调用

l         因此,可以在sessionCreated()方法中做一些Session创建后的初始化工作;而在sessionDestroyed()方法中做一些Session销毁前的清除工作

l         要注册监听器,可以在web.xml中配置:

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
   
<web-app>
  <context-param>
    <param-name>uji.handleException.wrap</param-name>
    <param-value>true</param-value>
  </context-param>
 
  <listener> 
       <listener-class> 
              com.fujitsu.eFrame.eftool.SessionListener 
       </listener-class> 
  </listener>
  
  <session-config>
    <session-timeout>1</session-timeout>
  </session-config>
 
  <taglib>
    <taglib-uri>uji-taglib</taglib-uri>
    <taglib-location>/WEB-INF/ujiall.tld</taglib-location>
  </taglib>
</web-app>

l         APC缺省使用web-app_2_2.dtd,是不支持<listener>标记的,需要改为web-app_2_3.dtd

l         使用<listener>标记注册需要监听的监听器

l         上面的例子同时还使用<session-timeout>标记指定缺省的Session的有效时间,注意是以分钟为单位的




相关文章

相关软件