在APC中使用Session

类别:Java 点击:0 评论:0 推荐:

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对象

Ø         由DispatchContext的getSessionProfile方法获得HttpSessionProfile对象(要强制转换,因为SessionProfile是HttpSessionProfile的抽象父类)

Ø         然后由HttpSessionProfile的getSession()方法获得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的有效时间,注意是以分钟为单位的

本文地址:http://com.8s8s.com/it/it14184.htm