简体   繁体   English

如何更新session.setAttribute(name,value)值,其名称相同?

[英]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.

相关问题 HttpSession - 如何获取session.setAttribute? - HttpSession - how to get the session.setAttribute? 使用session.setAttribute复制会话对象 - Session Object replication with session.setAttribute session.setAttribute(“ authManager”,manager)抛出NotSerializableException - session.setAttribute(“authManager”, manager) throws a NotSerializableException 在已部署的系统中使用session.setAttribute - Using session.setAttribute in a deployed system setAttribute()之后,名称属性的设置值中断 - Setting value of name attribute breaks after setAttribute() session.setAttribute和request.setAttribute之间有什么区别? - What's the difference between session.setAttribute and request.setAttribute? 如何解决SONAR指出的session.setAttribute()中的信任边界冲突? - How to Solve Trust Boundary Violation in session.setAttribute() point out by SONAR? 当数据库中有两列同名时如何更新表值 - How to update a table value when there are two columns with the same name in a database 做“model.addAttribute()”和“session.setAttribute()”的区别 - difference in doing “model.addAttribute()” and “session.setAttribute()” 如何解析name = value ^^ name = value ^^ name = value - How to parse name=value^^name=value^^name=value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM