我在处理会话中的对象时遇到了问题。
我将对象存储在这样的会话中。假设object是对象的名称。我在我的动作类中这样做:
if(object!=null)
{
session.settAttribute("objectName",object);
return mapping.findForward("success");
}
else
{
return mapping.findForward("failure");
}
我将成功和失败映射到同一个jsp页面。我检查了
if(session.getAttribute("objectName")!=null)
{
object= (SomeObjectClass)session.getAttribute("objectName");
}
if(object!=null)
{
//Do this
}
else
{
//Do that
}
现在我的问题来了。在会话中第一次设置对象时没有问题。当我从两个不同的浏览器调用此动作类时,我遇到了一个问题,同时我转到另一个案例的一部分,如果是一个案例的一部分。我相信这是因为会话不是线程安全的。有没有解决方案?
答案 0 :(得分:1)
你提到你试图在两个浏览器之间看到相同的信息...如果你想要分享的信息是“全局的”(即应该对应用程序的所有用户都相同,你应该将信息存储在应用程序范围中,而不是会话范围。(有关范围的说明,请参阅http://java.sun.com/developer/onlineTraining/JSPIntro/contents.html#JSPIntro5。)例如:
ServletContext servletContext = getServlet().getServletContext(); //"application scope"
SomeObjectClass object = (SomeObjectClass) servletContext.getAttribute("objectName");
if(object !=null){
//Do this
} else {
//Do that
}
如果您有帐户和登录机制,并且您希望相同的登录信息在两个不同的浏览器中看到相同的信息,那么您会遇到其他问题。在这种情况下,信息需要存储在“数据库”中(不一定是rdbms,可能是应用程序范围!,具体取决于您对持久性的需求),并且需要使用用户在操作类中检索信息可以存储在会话,cookie等中的信息
//get the user info from the session, cookies, whatever
UserInfoObject userInfo = getUserInfo(request);
//get the object from the data store using the user info
SomeObjectClass object = getObjectForUser(userinfo);
if(object !=null){
//Do this
} else {
//Do that
}
答案 1 :(得分:0)
当您从其他浏览器访问操作/页面时,您将创建一个新会话。在现代浏览器中,您可以在标签或视图之间共享会话。与更多浏览器共享会话的唯一方法是在URL中使用jSessionid param。