当我们在使用DWR的反向AJax是,每次页面的刷新都会产生一个ScriptSession(SS),但是我们确无从对过期的SS进行即使的销毁,虽然可以通过在每个页面访问时,自动执行某个方法,来销毁那些当前用户的非有效SS,但是这样也使得我们在代码管理上带来非常麻烦的问题.
DWR3的诞生终于给我们提供了ScritpSessionLintener(SSL)接口
本文,主要讲解如何使用ScriptSession接口.
DWR支持在Web.XML当中,配置扩展.
<init-param>
<param-name>*</param-name>
<param-value>*</param-value>
</init-param>
但是,经过几次的实验和摸索,SSL在当中配置后,管理器会进行有效构造,但是SS在创建和销毁时,并不会执行继承了ScriptSessionListner类的相关方法.(或许是我自己项目的问题.)
经过自己的研究发现,在ScriptSessionManager类中,包含了AddScriptSessionListener方()法.这样给使用SSL带来了方便
我们只需要在Web.XML文件中配置一个自定义的ScrptSessionManager
public class CustomScriptSessionManager extends org.directwebremoting.impl.DefaultScriptSessionManager {
public CustomScriptSessionManager(){
try {
this.addScriptSessionListener(new CustomScriptSessionListener());
} catch (Exception e) {
e.printStackTrace();
}
}
}
*CustomScriptSessionListener 为一个实现了ScriptSessionListener接口的实体类.
通过这种方式,将SS管理程序注射近SSM内,让管理器在SS状态发生变化时,即使通过SSL进行处理
如何使得每个用户的SS都是新鲜的,每个人都有自己的方法,我在自己的管理程序内使用一个
将每个用户的Session内存放当前ScriptSession进行绑定,当用户生成一个新的ScriptSession时,根据用户的Session,并且将旧的ScriptSession进行主动销毁,并更新Session.
这样,我们随着高效的保证每个用户只有一个ScriptSession存在于内存当中,使得我们的程序更加高效.
package com.dwr;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 在线ScriptSession(SSL)监听管理器.
* 在SSL中,每当DWR工厂在生成一个新的ScriptSession(SS)时,将被SSL捕获
* SSL对捕获的SS进行初始化赋值
* 其中在SS属性中的SessionLabel.CurrentSesionID赋予当前用户的SessinID
* 并在改用户的Session属性中的SessionLabel.CurrentScriptSessionID赋予当前的SSL
* 然后在SessionLabel.CurrentPage中赋予当前SS的操作界面地址
*
* 并且开始激活SSL插件中的sessionCreated方法
*
* 当
* @author 熊浩华 - ibmsz
*
* 2009-8-18:下午03:11:55-
*/
public class CustomScriptSessionListener implements ScriptSessionListener {
public static final HttpSessionLabel SessionLabel = new HttpSessionLabel();
private Collection<IListenerMessage> collection = null;
public CustomScriptSessionListener() throws Exception{
System.out.println("开始构造SSL");
collection = new Vector<IListenerMessage>();
SAXReader reader = new SAXReader();
Document document = reader.read(this.getClass().getResource("ScriptSessionListener.xml"));
Element rootElement = document.getRootElement();
Iterator it = rootElement.elementIterator("listener");
while(it.hasNext() && it != null){
Element element = (Element)it.next();
String classPath = element.getTextTrim();
if(classPath == null || classPath.trim().equals("")){
continue;
}
Class cls = Class.forName(classPath);
Object object = cls.newInstance();
if(object instanceof IListenerMessage){
this.collection.add((IListenerMessage)object);
}
}
}
@SuppressWarnings("deprecation")
public final void sessionCreated(ScriptSessionEvent sSessionEvent) {
// System.out.println("创建新的ScriptSession时执行");
ScriptSession scriptSession = sSessionEvent.getSession(); //获取新创建的SS
WebContext webContext = WebContextFactory.get();
HttpServletRequest httpServletRequest = webContext.getHttpServletRequest();
HttpSession httpSession = httpServletRequest.getSession(); //获取构造SS的用户的HttpSession
ScriptSession currSession = (ScriptSession)httpSession.getAttribute(SessionLabel.getCurrentScriptSessionID());
if(currSession != null){
currSession.invalidate();
}
for(IListenerMessage message : this.collection){
message.sessionCreated(sSessionEvent);
}
httpSession.setAttribute("DWR3.CurrPath", scriptSession.getPage());
httpSession.setAttribute("DWR3.CurrScriptSession", scriptSession);
Collection<ScriptSession> sSCollection = webContext.getScriptSessionsByPage(scriptSession.getPage());
for(ScriptSession session : sSCollection){
if(session.getAttribute(SessionLabel.getCurrentSesionID()) == null){
continue;
}
for(IListenerMessage message : this.collection){
message.sendCreateMessage(scriptSession,session);
}
}
scriptSession.setAttribute(SessionLabel.getCurrentSesionID(), httpSession.getId());
}
@SuppressWarnings("deprecation")
public final void sessionDestroyed(ScriptSessionEvent sSessionEvent) {//销毁一个ScriptSession时执行
ScriptSession scriptSession = sSessionEvent.getSession();
/*
Object username = (Object)scriptSession.getAttribute(SessionLabel.getCurrentScritpUserName());
if(username == null){
username = "";
}
*/
for(IListenerMessage message : this.collection){
message.sessionDestroyed(sSessionEvent);
}
Collection<ScriptSession> collection = ServerContextFactory.get().getScriptSessionsByPage(scriptSession.getPage());
for(ScriptSession session : collection){
String scritpAttrID = (String)session.getAttribute(SessionLabel.getCurrentSesionID());
if( scritpAttrID != null){
for(IListenerMessage message : this.collection){
message.sendDestroyMessage(scriptSession,session);
}
}
}
}
public static class HttpSessionLabel{
private final String CurrentScriptSessionID = "DWR3.CurrScriptSession";
private final String CurrentScritpUserName = "DWR.Chat.UserName";
private final String CurrentSesionID = "DWR3.CurrSessionID";
private final String CurrentPage = "DWR3.CurrPath";
/**
* 获取当前SessionScript的在线页面
* @return currentPage
*/
public String getCurrentPage() {
return CurrentPage;
}
/**
* 获取Session中的当前ScriptSession的ID
* @return currentScriptSessionID
*/
public String getCurrentScriptSessionID() {
return CurrentScriptSessionID;
}
/**
* 获取当前用户名称
* @return currentScritpUserName
*/
public String getCurrentScritpUserName() {
return CurrentScritpUserName;
}
/**
* 获取ScriptSession中的HttpSessionID
* @return currentSesionID
*/
public String getCurrentSesionID() {
return CurrentSesionID;
}
}
}
以上代码是从一个通过DWR实现的及时在线聊天系统中抽取出来的ScriptSession监听器.
红色标注部分为管理SS的程序段
全案例源代码下载
分享到:
相关推荐
DWR(Direct Web Remoting)是一种Java库,它允许JavaScript在客户端与服务器端进行直接的交互,从而实现在Web应用程序中的Ajax功能。DWR的主要目标是简化前后端的数据交换,提高用户体验,使得Web应用能够像桌面...
总的来说,这份“DWR中文帮助文档”是学习和使用DWR的关键资源,它能够帮助开发者高效地理解和应用DWR,实现JavaScript与Java后端的无缝交互,创建高性能的Web应用。通过阅读和实践文档中的内容,开发者可以深入理解...
4. **缓存管理**:DWR支持缓存策略,可以有效地减少不必要的网络传输,提高应用性能。 5. **批量调用和异步处理**:DWR允许开发者一次性调用多个远程方法,并且可以设置异步执行,优化了处理大量请求的效率。 6. *...
DWR(Direct Web Remoting)是一种Java库,用于在Web应用程序中实现JavaScript和服务器端Java代码之间的双向通信。DWR允许开发者在不刷新整个页面的情况下更新网页部分,从而提供了类似于桌面应用的用户体验。这个...
### DWR中处理List知识点详解 #### 一、DWR简介 DWR(Direct Web Remoting)是一种简化Ajax开发的框架,它使得JavaScript能够直接调用服务器端的Java方法,从而实现更简单、更直接的远程调用。通过DWR框架,开发者...
### DWR中文文档知识点...综上所述,DWR不仅提供了一种高效简便的方式来实现Ajax应用,还支持与多种流行框架的无缝集成,大大提高了开发效率。通过合理配置DWR的相关文件,开发者可以轻松地构建出功能强大的Web应用。
- **4.7.2 在JSP中导入脚本**:在WebWork的JSP页面中使用DWR。 **4.8 DWR与Acegi** - Acegi是Spring Security的前身,DWR可以与Acegi结合使用。 - **4.8.1 问题提出**:指出在安全方面可能遇到的问题。 - **4.8.2 ...
DWR(Direct Web Remoting)是一种...通过学习这份详尽的DWR中文文档,开发者可以深入了解DWR的工作原理,熟练运用其功能,构建高效、用户体验优秀的Web应用。无论是初学者还是经验丰富的开发者,都能从中受益匪浅。
DWR(Direct Web Remoting)是一种Java库,用于在Web应用程序中实现实时的JavaScript到服务器端Java对象的通信。这个“DWR中文教程(外带DWR包)”为初学者提供了一个全面的入门指南,帮助理解并掌握DWR的基本概念和...
**Direct Web Remoting (DWR)** 是一个开源的Java库,它允许JavaScript在浏览器和Java服务器之间进行安全、高效的通信。DWR使得Web应用程序能够利用AJAX技术,提供动态、实时的用户界面,无需页面刷新即可与服务器...
在权限管理中,DWR可实现实时的权限校验,比如用户在前端界面上进行操作时,后台可以通过DWR实时验证其是否有相应的操作权限。 在OA权限管理中,通常涉及到以下几个关键知识点: 1. **角色与权限**:系统中的用户...
在DWR2中,使用ScriptSession管理器来区分不同用户并跟踪他们的会话。当用户登录时,你可以通过添加ScriptSessionListener到ScriptSessionManager,并在sessionCreated方法中设置用户的标识。然而,在DWR3中,文档的...
DWR(Direct Web Remoting)是一个开源的Java...通过学习这份DWR中文文档,你将能够理解和掌握DWR的工作原理,有效地在Web应用中使用DWR进行AJAX开发,提升用户体验,并了解如何在实际项目中安全、高效地运用这一技术。
9. **集成Spring框架**:如果你的项目使用了Spring,教程可能会介绍如何将DWR与Spring集成,实现依赖注入和事务管理。 10. **最佳实践**:教程会分享一些使用DWR的最佳实践,如避免全局变量、正确处理异步回调等,...
例如,`Engine`类用于初始化和管理DWR实例,`Allow`类用于控制哪些Java类和方法可以被JavaScript访问。 4. **安全**:介绍如何确保DWR通信的安全性,如使用HTTPS,设置访问控制,以及防止跨站脚本攻击(XSS)和跨站...
在IT行业中,权限管理是构建安全、高效应用的关键部分,特别是在企业级系统中。本项目以"Hibernate+Dwr实现权限管理"为主题,利用这两种强大的技术来构建一套完整的权限控制系统。接下来,我们将深入探讨这两个技术...
DWR,全称Direct Web Remoting,是一个JavaScript框架,旨在简化Web应用程序中的客户端与服务器端交互。它允许开发者在浏览器中直接调用Java方法,就像操作本地对象一样,从而实现AJAX(Asynchronous JavaScript and...