-1

I need to show the previously executed command in the table in the webpage for the current session. But, I am getting only one the last executed command in the table. I don't know what I am missing...

ArrayList commands = (ArrayList)session.getAttribute("commands");
String cmd = (String)request.getAttribute("text1");
commands.add(cmd);
session.setAttribute("commands",commands);

1 Answers1

0

May be an explanation error only, but the 4th line should ideally be

session.setAttribute("commands",commands);

Kaustabh
  • 37
  • 6
  • Please add how you are getting the session, also how you are generating the table. This piece of code looks fine to me. May be the session is being reset at some point .. may be .. just trying to think aloud – Kaustabh Feb 25 '19 at 05:06
  • I am getting an empty arraylist initially like this, ArrayList commands = (ArrayList)session.getAttribute("cmd")==null?new ArrayList():(ArrayList)session.getAttribute("cmd"); I am showing the array list as table using for loop as in the next comment – Sabari Krishnan M Feb 25 '19 at 05:22
  • int len = commands.size(); if(len > 0) { for(int i=0;i <% } } – Sabari Krishnan M Feb 25 '19 at 05:24
  • ArrayList commands = (ArrayList)session.getAttribute("cmd")==null?new ArrayList():(ArrayList)session.getAttribute("cmd"); This line has the cmd/commands mismatch, I hope you have already adjusted that. I think the problem is your session is being recreated with every request thts why you are always getting a new ArrayList from this line. – Kaustabh Feb 25 '19 at 05:45
  • Yeah. Correct. The session is recreated every time... How to get the older session? – Sabari Krishnan M Feb 25 '19 at 05:49
  • Where might be the error for session being recreated every time – Sabari Krishnan M Feb 25 '19 at 06:14
  • Check the options here, there are multiple threads here where this topic was discussed https://stackoverflow.com/questions/2138245/session-is-lost-and-created-as-new-in-every-servlet-request – Kaustabh Feb 25 '19 at 06:35