ashlesha
ashlesha

Reputation: 109

Values Lost while redirecting to the same jsp ( Without Ajax)

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);

Upvotes: 0

Views: 1935

Answers (1)

BalusC
BalusC

Reputation: 1109132

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:

Upvotes: 3

Related Questions