JSP Validation and Redirection: How do I validate form input and redirect errors to the original page?

I take a class on JSP and I have a task ... we need to write a JSP page that enters user input, validates the input and then redirects it to another website. To be more precise, we were asked to implement a rudimentary version of the FareFinder functionality of the Amtrak website.

There are two main objectives in this assignment:
(a) write a JSP that acts as a middleware.
and (b) write a JSP that validates form data.

I have a general question about the principles of verification. I currently have a JSP that has a form and a submit button. When the user clicks the submit button, I forward them to Validate.jsp. Validate.jsp then validates the data, and if the login is ok, it automatically redirects the request to the Amtrak website with all the parameters filled in.

FareFinder.jsp β†’ Validate.jsp β†’ Amtrak
(click on the file name to see all my code in the paste)

In short, the main thing I do is FareFinder.jsp:

<FORM METHOD=POST ACTION="Validate.jsp"> <!-- all the input fields are up here --> <P><INPUT TYPE=SUBMIT></P> </FORM> 

The main thing I do in Validate.jsp is:

 <%@ page import="java.util.*" import="java.io.*"%> <% // retreive all the parameters String origin = request.getParameter("_origin"); String depmonthyear = request.getParameter("_depmonthyear"); String depday = request.getParameter("_depday"); String dephourmin = request.getParameter("_dephourmin"); String destination = request.getParameter("_destination"); String retmonthyear = request.getParameter("_retmonthyear"); String retday = request.getParameter("_retday"); String rethourmin = request.getParameter("_rethourmin"); String adults = request.getParameter("_adults"); String children = request.getParameter("_children"); String infants = request.getParameter("_infants"); String searchBy = request.getParameter("_searchBy"); // validate the data // redirect to Amtrak or back to FareFinder.jsp %> 

I have a few questions:
How can I go back to FareFinder.jsp from Validate.jsp and reflect the errors found on the check page?
As soon as I find errors - do I redirect the response back to FareFinder.jsp?
How can I pass the error (s) back to FareFinder.jsp?

The general answer will also be good, but I give the code as an example.

Note: validation must be done on the server side and I cannot use javascript.

+7
java validation jsp
source share
1 answer

OK, according to the comments, Servlets are also covered. Now create one and implement doPost() as it should (semi pseudo):

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String, String> errors = new HashMap<String, String>(); String origin = request.getParameter("origin"); if (origin does not validate) { errors.put("origin", "Put error message here."); } // Repeat for all parameters. if (errors.isEmpty()) { // No errors, redirect to Amtrak. response.sendRedirect("http://amtrak.com"); } else { // Put errors in request scope and forward back to JSP. request.setAttribute("errors", errors); request.getRequestDispatcher("FareFinder.jsp").forward(request, response); } } 

Map this servlet in web.xml to the url-pattern from /validateFare so that you can call it using http://example.com/context/validateFare .

Now do something like this in the JSP:

 <form action="validateFare" method="post"> <label for="origin">Origin</label> <input id="origin" name="origin" value="${param.origin}"> <span class="error">${errors.origin}</span> <!-- Repeat other fields here. --> </form> 

You see that the action form already points to the servlet. The ${param.origin} values ​​in the input values ​​keep the value represented by making request.getParameter("origin") under the hoods. ${errors.origin} display any related error message, making approximately pageContext.findAttribute("errors").get("origin") .

That should get you started :) Good luck.

+9
source share

All Articles