I have the code below in a servlet (page1) and I want after pressing Save to go to a second servlet (page2), read the content written in the form of page1 and append them in a r
You can use either or the session scope to remember previously saved data. E.g.
or
request.getSession().setAttribute("question1answer", 42);
The passed data is the subsequent request available as
String question1answer = request.getParameter("question1answer");
or
Integer question1answer = (Integer) request.getSession().getAttribute("question1answer");
The disadvantage of hidden inputs is that it produces quite some boilerplate code and that the enduser can easily guess/manipulate it. The disadvantage of the session scope is that it's shared across all requests within the same session (and thus may interfere when the enduser has the same page open in multiple browser windows/tabs). To combine best of both worlds, you can on 1st request generate a long and unique key which you use as a key to store all the associated data in the session scope and pass that key as hidden request parameter.
E.g. in first request
String key = UUID.randomUUID().toString();
request.setAttribute("key", key);
List answers = new ArrayList();
request.getSession().setAttribute(key, answers);
// ...
answer.add(question1answer);
and in HTML
and in all subsequent requests
String key = request.getParameter("key");
request.setAttribute("key", key);
List answers = (List) request.getSession().getAttribute(key);
// ...
answers.add(question2answer); // question3answer, question4answer, etc.