I need to display the data from a servlet in a jsp page.
At the moment I have this:
SERVLET:
Object data = text;
request.setAttribute("data", data);
request.getRequestDispatcher("index.jsp").forward(request, response);
JSP:
<p>result: ${data}</p>
In the JSP there is a simple text box and a send button.
At the moment when I push the send button the response is being overwritten ervery time.
How can I do it that after every search I see the result in a new line?
I want also see the previous searches…
Thanks a lot!
You’ve got a couple of options:
Send over the current value to the server, do the search and append the new result there, and send back the whole string to the JSP in the
Requestto display it all again. You’ll have to wrap the value in an<input>tag, possibly<input type="hidden">if you still want to showdatain a<p>and not as an input field.JSP:
Servlet:
Store all values of
datain the session scope, and just append to it from your servlet. The JSP would have to output the value from the session scope instead of the request. In this example values are stored in a unique concatenatedString. It woukld probably be nicer to store each value ofdatain a data structure, such as aList, and just iterate over it in the JSP so the separator remains in the view.JSP:
Servlet: