Java 如何更新session.setAttribute(名称、值)值,其中名称相同?

Java 如何更新session.setAttribute(名称、值)值,其中名称相同?,java,servlets,Java,Servlets,在这种情况下,我需要更新名称保持不变的setAttribute的值。考虑下面的情况,例如:假设我有三个JSP:ABCJSP,XYZ.JSP,PQR.JSP。现在,首先运行abc.jsp,然后将控制转发到xyz.jsp,然后转发到pqr.jsp。现在,在执行pqr.jsp之后,再次将控件返回到xyz.jsp,并在setAttribute处更新值。 abc.jsp: ArrayList<Books> getSupplyStatus=new ArrayList<Books>()

在这种情况下,我需要更新名称保持不变的setAttribute的值。考虑下面的情况,例如:
假设我有三个JSP:ABCJSP,XYZ.JSP,PQR.JSP。现在,首先运行abc.jsp,然后将控制转发到xyz.jsp,然后转发到pqr.jsp。现在,在执行pqr.jsp之后,再次将控件返回到xyz.jsp,并在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);
}
session=request.getSession(false);
ArrayList<Books> getSupplyStatus=(ArrayList<Books>) session.getAttribute("UpdatedBooklist");
// some operations & forward to 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.
ArrayList getSupplyStatus=new ArrayList();
JavaBean=newJavaBean();
session=request.getSession(false);
getSupplyStatus=javaBean.getSupplyStatus(memberID)//它返回一个数组列表
如果(!getSupplyStatus.isEmpty())
{
setAttribute(“UpdatedBooklist”,getSupplyStatus);
getRequestDispatcher(“xyz.jsp”).forward(请求,响应);
}
xyz.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);
}
session=request.getSession(false);
ArrayList<Books> getSupplyStatus=(ArrayList<Books>) session.getAttribute("UpdatedBooklist");
// some operations & forward to 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.
session=request.getSession(false);
ArrayList getSupplyStatus=(ArrayList)session.getAttribute(“UpdatedBooklist”);
//一些操作&转发到pqr.jsp
pqr.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);
}
session=request.getSession(false);
ArrayList<Books> getSupplyStatus=(ArrayList<Books>) session.getAttribute("UpdatedBooklist");
// some operations & forward to 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.
//在此jsp中,将生成新的ArrayList
//我需要将“UpdatedBooklist”的值与绑定(&I)
//在abc.jsp中设置,
//并且必须覆盖上一个值&然后再次转发到xyz.jsp
//在xyz.jsp中,我们接收更新的值。
再次使用setAttribute()将创建并调用必要的生命周期方法

如果对象已绑定到此实现HttpSessionBindingListener的同名会话,则将调用其HttpSessionBindingListener.valueUnbound方法


您还可以再次使用和设置具有相同名称的属性。如果“更新”的意思是要更新对象而不是替换对象,则使用
getAttribute()
获取属性,并调用将改变对象的方法。

请进一步澄清您的问题。