jros
jros

Reputation: 854

Java servlet redirect has wrong URL and uses wrong HTTP method

I have a form that is submitting a POST request to a servlet defined by @WebServlet("/SignUp").

I also have checks on the client side to make sure all of the form inputs are populated, but if they sneak by that and submit the form with an empty input I'd like the servlet to send them back /Home (i.e. index.jsp) as a fallback.

I have the following for my SignUp servlet's doPost():

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    final String METHODNAME = "doGet";
    logger.entering(CLASSNAME, METHODNAME);

    String email = request.getParameter("email");
    String pass = request.getParameter("password");
    String cpass = request.getParameter("confirmPassword");

    String forwardJsp = "";
    if (StringUtils.isNotBlank(email) && StringUtils.isNotBlank(pass)
            && StringUtils.isNotBlank(cpass)) {
        forwardJsp = "/SignUpMoreInfo.jsp";
    } else {
        // one of the fields was empty
        request.setAttribute("signupFieldWasEmpty", true);

        forwardJsp = "/Home";
    }

    request.getRequestDispatcher(forwardJsp).forward(request, response);
    logger.exiting(CLASSNAME, METHODNAME);
}

This all works on a basic level. If they neglect to enter a value (i have yet to do any real checking, like .trim() or invalid characters), then they are send back to index.jsp (which backed by the /Home servlet).

However, I have two problems with the way my code is working and I can't figure out how to get around them.

  1. My index.jsp is now being loaded as a POST request, rather than the usual GET. If the user refreshes the page, they get a client warning about refreshing the page on a POST request which is really ugly and cumbersome on the homepage.

  2. The URL in the browser still has the address http://localhost:8080/SignUp, when I was expecting it to be http://localhost:8080/Home. This is also ugly and kind of confusing to the user, as the url no longer properly represents the page they associate with the "Home" page of the site.

How do I fix these issues I have with my Java backend code while maintaining as much MVC as possible?

Upvotes: 0

Views: 1908

Answers (2)

Matt
Matt

Reputation: 3760

You can use the Post/Redirect/Get pattern to avoid this problem.

The servlet receives the Post request, processes the form, then sends a redirect to the desired page.

Upvotes: 0

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3759

You have to use redirect here instead to start a new request to /home :

Try this:

if (StringUtils.isNotBlank(email) && StringUtils.isNotBlank(pass) && StringUtils.isNotBlank(cpass)) {

    forwardJsp = "/SignUpMoreInfo.jsp";

} else {

    request.setAttribute("signupFieldWasEmpty", true);

    response.sendRedirect("/Home");
}

However the problem now is that you are going to lose this information signupFieldWasEmptyso if you want to keep it you have to add as a request param to the uri :

else {

    response.sendRedirect("/Home?signupFieldWasEmpty=true");

}

Upvotes: 1

Related Questions