关于DWR 2.0的Reverse Ajax特性的一个问题

cooljam 2007-05-31 04:01:49
DWR 2.0的Reverse Ajax功能确实非常强大,但是目前还是有一些问题,不知是我程序的问题,还是确实是BUG。

发现问题:在服务器端java代码中需要用到HttpSession存取变量,但是用一个扩展了HttpSessionListener的监听器发现,每次打开一个新的IE窗口第一次调用已经写好的服务器方的方法存取一个HttpSession中的变量,就会创建2个HttpSession。这样有时会导致后面的程序有一定几率取到不同的HttpSession而找不到原已经存好的变量。

通过跟踪源码,发现在dwr的包中的org.directwebremoting.dwrp.PollHandler的notifyThreadsFromSameBrowser(HttpServletRequest request, String scriptId)使用过HttpSession,在自己写的程序里面也同样使用了HttpSession,这样同一个连接就有了2个不同的HttpSession,但是不对呀,同一个进程的浏览器怎么会在服务器端创建2个不同的Session呢?

再次跟踪源码,发现在页面上加入dwr.engine.setActiveReverseAjax(true);这个js代码后,页面会定期访问<myApp>/dwr/call/plainpoll/ReverseAjax.dwr 60秒一次,在页面第一次调用服务器方的方法的时候也会访问<myApp>/dwr/call/plainpoll/<类名>.<方法名>.dwr,而这2个连接使用的是不同jsessionId,这样直接导致服务器方在使用HttpSession的时候创建了2个不同Session。

这样一个连接多个httpSession无疑是一种资源的浪费,而且在程序接下来的处理过程中使用有可能一下用这个Session,一下用那个Session,导致程序出错,不知道怎么解决这个问题。
...全文
1223 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
knowledge_Is_Life 2008-04-30
  • 打赏
  • 举报
回复
接分是王道!
czwlucky 2008-03-19
  • 打赏
  • 举报
回复
哦?还有这种事?我试试去...
cooljam 2007-05-31
  • 打赏
  • 举报
回复
to jiang117(天涯浪客)
可是我的问题不是这个啊,在第一次访问服务器上的方法,而方法会使用到HttpSession,但是同一个进程的浏览器发出的访问却在服务器端创建了2个不同HttpSession,这样就麻烦了~
jiang117 2007-05-31
  • 打赏
  • 举报
回复
服务器端java代码中的方法
getXXXX(String arg1,HttpSession session){}
客户端调用getXXXX(arg1)即可,HttpSession 参数DWR自动填充
cooljam 2007-05-31
  • 打赏
  • 举报
回复
这个是页面的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>DWR Thin Chat Version 2.0</title>
<script type='text/javascript' src='../dwr/engine.js'> </script>
<script type='text/javascript' src='../dwr/interface/JavaChat.js'> </script>
<script type='text/javascript' src='../dwr/util.js'> </script>
<script type="text/javascript">
function sendMessage() {
JavaChat.addMessage(dwr.util.getValue("text"));
}
</script>
<link rel="stylesheet" type="text/css" href="../generic.css" />
</head>

<body onload="dwr.engine.setActiveReverseAjax(true);">
<div id="page-title">[
<a href="http://getahead.org/dwr/">DWR Website</a> |
<a href="..">Web Application Index</a>
]</div>

<h1>Java Chat</h1>

<p>This is a very simple chat demo that uses reverse ajax to collect messages
and server-side browser manipulation to update the pages with the results.</p>

<p>
Your Message:
<input id="text" onkeypress="dwr.util.onReturn(event, sendMessage)"/>
<input type="button" value="Send" onclick="sendMessage()"/>
</p>
<hr/>

<ul id="chatlog" style="list-style-type:none;">
</ul>

</body>
</html>



运行程序,打开一个浏览器访问,看log就会发现第一次访问和第二次访问的HttpSession的ID是不相同的~~~~~
cooljam 2007-05-31
  • 打赏
  • 举报
回复
付上源码(这是dwr2的src包中的例子JavaChat,我稍做修改)

package org.getahead.dwrdemo.chat;

import java.util.Collection;
import java.util.LinkedList;

import javax.servlet.http.HttpSession;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import org.directwebremoting.util.Logger;

/**
* @author Joe Walker [joe at getahead dot ltd dot uk]
*/
public class JavaChat
{
/**
* @param text The new message text to add
*/
public void addMessage(String text)
{
// Make sure we have a list of the list 10 messages
if (text != null && text.trim().length() > 0)
{
messages.addFirst(new Message(text));
while (messages.size() > 10)
{
messages.removeLast();
}
}

WebContext wctx = WebContextFactory.get();
//这里是自己加入的代码
HttpSession hSn = wctx.getSession();
if(hSn.getAttribute("No")==null){
hSn.setAttribute("No", getID());
}
log.info((Long)hSn.getAttribute("No")+" : " + hSn.getId());
//到这里结束

String currentPage = wctx.getCurrentPage();

// Clear the input box in the browser that kicked off this page only
Util utilThis = new Util(wctx.getScriptSession());
utilThis.setValue("text", "");

// For all the browsers on the current page:
Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);

// Clear the list and add in the new set of messages
utilAll.removeAllOptions("chatlog");
utilAll.addOptions("chatlog", messages, "text");
}

//这里取得一个访问者的ID
private synchronized long getID(){
return No++;
}

/**
* The current set of messages
*/
private LinkedList messages = new LinkedList();

/**
* The log stream
*/
protected static final Logger log = Logger.getLogger(JavaChat.class);

private long No = 0;//访问者的ID序号
}

52,792

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧