[英]How to update session.setAttribute(name,value) value, where the name is same?
I have a situation where I need to update the value of a setAttribute where the name remains same. 我有一种情况,我需要更新名称保持不变的setAttribute的值。 Consider the following situation as example-
考虑以下情况作为示例 -
Suppose I have three JSP: abc.jsp, xyz.jsp, pqr.jsp. 假设我有三个JSP:abc.jsp,xyz.jsp,pqr.jsp。 Now at first abc.jsp runs then control forward to xyz.jsp & then forward to pqr.jsp.
现在首先运行abc.jsp然后控制前进到xyz.jsp然后转发到pqr.jsp。 Now after execute pqr.jspthe control back to xyz.jsp again with a updated value at setAttribute.
现在执行pqr.jspt后,再次使用setAttribute中的更新值控制回xyz.jsp。
abc.jsp: 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: xyz.jsp:
session=request.getSession(false);
ArrayList<Books> getSupplyStatus=(ArrayList<Books>) session.getAttribute("UpdatedBooklist");
// some operations & forward to pqr.jsp
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.
Using setAttribute() again will replace the value and call the necessary lifecycle methods. 再次使用setAttribute()将替换该值并调用必要的生命周期方法。
If an object was already bound to this session of this name that implements HttpSessionBindingListener, its HttpSessionBindingListener.valueUnbound method is called.
如果某个对象已绑定到此名称的实现HttpSessionBindingListener的会话,则会调用其HttpSessionBindingListener.valueUnbound方法。
You can also use removeAttribute() and set an attribute with the same name again. 您还可以使用removeAttribute()并再次设置具有相同名称的属性。 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. 如果通过'update'表示您希望对象更新而不是替换,则使用
getAttribute()
获取属性并在其上调用将改变对象的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.