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.
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.
The URL in the browser still has the address
http://localhost:8080/SignUp, when I was expecting it to behttp://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?