如何更新session.setAttribute(name,value)值,其名称相同? [英] How to update session.setAttribute(name,value) value, where the name is same?

查看:697
本文介绍了如何更新session.setAttribute(name,value)值,其名称相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况需要更新名称保持不变的setAttribute的值。考虑以下情况作为示例 - 假设我有三个JSP:abc.jsp,xyz.jsp,pqr.jsp。现在首先运行abc.jsp然后控制前进到xyz.jsp&然后转发到pqr.jsp。现在执行pqr.jspt后再次使用setAttribute的更新值再次控制回xyz.jsp。

abc.jsp:

I have a situation where I need to update the value of a setAttribute where the name remains same. Consider the following situation as example-
Suppose I have three JSP: abc.jsp, xyz.jsp, pqr.jsp. Now at first abc.jsp runs then control forward to xyz.jsp & then forward to pqr.jsp. Now after execute pqr.jspthe control back to xyz.jsp again with a updated value at setAttribute.
abc.jsp:

ArrayList<Books> getSupplyStatus=new ArrayList<Books>();
JavaBean javaBean=new JavaBean();
session=request.getSession(false);
getSupplyStatus=javaBean.getSupplyStatus(memberID); //It returns a ArrayList 
if(!getSupplyStatus.isEmpty())
{
  session.setAttribute("UpdatedBooklist", getSupplyStatus);
  request.getRequestDispatcher("xyz.jsp").forward(request, response);
}

xyz.jsp:

session=request.getSession(false);
ArrayList<Books> getSupplyStatus=(ArrayList<Books>) session.getAttribute("UpdatedBooklist");
// some operations & forward to pqr.jsp

pqr.jsp:

// in this jsp new ArrayList<Books> will be prodeuced
// & I need to bound the value of "UpdatedBooklist" with 
// which is set in abc.jsp,
// and previous value must be override & then forward to xyz.jsp again
// In xyz.jsp we recieve the updated value.


推荐答案

再次使用setAttribute()将 replace值并调用必要的生命周期方法。

Using setAttribute() again will replace the value and call the necessary lifecycle methods.


如果一个对象已绑定到此名称的会话,该会话实现了HttpSessionBindingListener ,调用它的HttpSessionBindingListener.valueUnbound方法。

If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.

你也可以使用 removeAttribute()并再次设置具有相同名称的属性。如果通过'update'表示您希望对象更新而不是替换,则使用 getAttribute()获取属性,并在其上调用将改变对象的方法。

You can also use removeAttribute() and set an attribute with the same name again. If by 'update' you mean you want the object updated and not replaced, then get the attribute with getAttribute() and call methods on it that will mutate the object.

这篇关于如何更新session.setAttribute(name,value)值,其名称相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆