在APC中使用Session

博客介绍了Session的使用和监听方法。使用时,遵循MVC模式在Handler中实现,通过DispatchContext上下文环境获取HttpSession对象,注意setTimeout()方法中Session超时时间以秒为单位。监听方面,可实现HttpSessionListener接口,在web.xml中配置监听器,指定缺省Session有效时间以分钟为单位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的有效时间,注意是以分钟为单位的

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值