0

I created a registration page (r.jsp) where we can enter user details. Upon clicking the submit button, the servlet is hit and it will perform the business logic. If successful, it redirects to x.jsp otherwise if it failed it redirects to r.jsp.

The problem I am having is when there is a failure and it redirects to r.jsp, data entered by the user is lost. I would like to retain it.

Below is the code I am performing in the servlet:

if (appResponse.getMessageStatus().equalsIgnoreCase(AppConstants.SUCCESS)) {
    request.setAttribute("message", appResponse.getMessage());
    url = "/x.jsp";
} else {
    request.setAttribute("message", appResponse.getMessage());
    url = "/r.jsp";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);
matts
  • 6,738
  • 1
  • 33
  • 50
ashlesha
  • 109
  • 1
  • 2
  • 12

1 Answers1

3

Just redisplay the submitted values from the request parameter map in input elements. The request parameter map is in JSP/EL available by ${param}.

<input name="foo" value="${fn:escapeXml(param.foo)}" />
<input name="bar" value="${fn:escapeXml(param.bar)}" />
<input name="baz" value="${fn:escapeXml(param.baz)}" />

Note that the JSTL fn:escapeXml() is mandatory to prevent XSS attacks. You can omit it, but then you've a XSS hole.

<input name="foo" value="${param.foo}" />
<input name="bar" value="${param.bar}" />
<input name="baz" value="${param.baz}" />

See also:


Unrelated to the concrete problem, you aren't redirecting at all. You're just forwarding. You should however actually be redirecting in case of a successful submit to avoid double submits on pressing F5 (as per PRG pattern).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am confused.. pls execuse if my question is wrong.. if I am redirecting to the same jsp page.. how can I set the two values you mentioned.. for instance and – ashlesha Feb 22 '13 at 19:37
  • The code in the answer is complete as is. Just change your code accordingly based on the answer. You don't need to prepare anything in the servlet. The `${param.foo}` will print the request parameter with name `foo` in JSP. You possibly only need to declare the JSTL `fn` taglib as `<%@taglib%>` in top of JSP. If you've never worked with JSTL before, click the "JSTL" link in my answer to learn how to install/use it. Again, note that you aren't redirecting at all. You're merely forwarding. See also the "See also" link on the subject. – BalusC Feb 22 '13 at 19:41