0

I am working for application where i have to send one object per request.And i have to store the whole data in a session variable.How can i do that? I am sending the data into controller and creating the object and then trying to store in a session ,but it is not working and this is my code

MemberDependentBO memberdepinfo = new MemberDependentBO();

        memberdepinfo.setFirstName(getMemberFirstName().getValue().toString());
        memberdepinfo.setLastName(getMemberLastName().getValue().toString());
        memberdepinfo.setMiddleName(getMemberMiddleName().getValue().toString());
        memberdepinfo.setPassport(getMemberPassportNumber().getValue().toString());

    List<MemberDependentBO> listOfMemberAndDep = new ArrayList<MemberDependentBO>();

        listOfMemberAndDep.add(memberdepinfo);

        httpSession.setAttribute("insuredInfo", listOfMemberAndDep);

        System.out.println("List Size :"+listOfMemberAndDep.size());

Every time when i click the button,the object is coming from the controller .but each time i am getting the size of the list is 1.Any Help will be appreciated

khaja firoz
  • 221
  • 2
  • 14
born2code
  • 9
  • 5
  • 1
    Well, you are re-declaring and initializing the `List` each time, what do you expect? You probably want to scope the `List` outside the method. – Mena Mar 29 '16 at 12:01
  • @Meena can you edit my code and give the right solution please?? – born2code Mar 29 '16 at 12:04
  • if I had an answer for you I would **post** one, not edit your question with my answer! However, the code fragment you posted is likely insufficient to provide you with a usable answer. As stated, you should consider re-thinking the scope of your `List` (i.e. outside the method). – Mena Mar 29 '16 at 12:06
  • @Meena tell me what you need to answer my question please – born2code Mar 29 '16 at 12:29
  • a minimal, self-contained example would do. For instance a streamlined version of [what I expect to be your] servlet class. – Mena Mar 29 '16 at 12:30
  • @Mena did not get you – born2code Mar 29 '16 at 12:34
  • please add some more code to get clarification on your question – khaja firoz Mar 29 '16 at 12:49

1 Answers1

0

First try to pull listOfMemberAndDep out of the current session. If it doesn't exist, create it, add memberdepinfo and save in session. If it does exist, just add memberdepinfo and re-save in session. Something like this....

List<MemberDependentBO> listOfMemberAndDep = (List<MemberDependentBO>)httpSession.getAttribute("insuredInfo");

if(listOfMemberAndDep == null){
    listOfMemberAndDep = new ArrayList<MemberDependentBO>();
}

MemberDependentBO memberdepinfo = new MemberDependentBO();
memberdepinfo.setFirstName(getMemberFirstName().getValue().toString());
memberdepinfo.setLastName(getMemberLastName().getValue().toString());
memberdepinfo.setMiddleName(getMemberMiddleName().getValue().toString());
memberdepinfo.setPassport(getMemberPassportNumber().getValue().toString();

listOfMemberAndDep.add(memberdepinfo);
httpSession.setAttribute("insuredInfo", listOfMemberAndDep);
System.out.println("List Size :"+listOfMemberAndDep.size());
MolonLabe
  • 168
  • 2
  • 8
  • Left the System.out in there for your immediate benefit but you generally would use a logging API (e.g. Log4J, slf4j) to gain more control over what's printed and when. – MolonLabe Mar 29 '16 at 12:47