views:

1404

answers:

1

I'm taking a class on JSP and I have an assignment... we have to write a JSP page that takes user input, validate the input and then forward it to a different web site. To be more precise, we were asked to implement a rudimentary version of the FareFinder functionality of Amtrak's web site.

There are 2 main purposes to this assignment:
(a) to write JSP which performs as middleware;
and (b) to write JSP which validates form data.

I have a general question about the principles of doing the validation. Currently I have a JSP that has a form and a submit button. When the user clicks on the submit button I forward them to Validate.jsp. The Validate.jsp will then validate the data and if the input is OK it will automatically redirect the request to the Amtrak web site with all the parameters filled out.

FareFinder.jsp -> Validate.jsp -> Amtrak
(click on the file name to see all my code in a pastie)

Briefly, the main thing that I'm doing 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'm doing in Validate.jsp:

<%@ 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 several questions:
How do I return to FareFinder.jsp from Validate.jsp and reflect the errors found in the validation page?
Once I have found errors- do I redirect the response back to FareFinder.jsp?
How could I transmit the error(s) back to FareFinder.jsp?

A generic answer would be fine too, but I'm giving my code as an example.

Note: the validation must be performed on the server side and I can't use javascript.

+1  A: 

OK, as per the comments, Servlets are covered as well. Now, create one and implement doPost() like follows (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 on an url-pattern of /validateFare so that you can invoke it by http://example.com/context/validateFare.

Now do in JSP something like:

<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 form action already points to the servlet. The ${param.origin} in input values will retain the submitted value by doing request.getParameter("origin") under the hoods. The ${errors.origin} will show any associated error message by roughly doing pageContext.findAttribute("errors").get("origin").

This must get you started :) Good luck.

BalusC
So technically speaking I could take the logic from the validateFare servlet and put it into the Validate.jsp in order to get the same result and avoid directly using a servlet... after all the jsp is a servlet, so it should have the same result.
Lirik
That's correct. Only keep in mind that you don't write any template text to the output in JSP! This includes linebreaks between `%>` and `<%`. It would potentially produce `IllegalStateException: response already committed` when you're trying to do a redirect/forward while the response has already been sent to the client due to template text. http://stackoverflow.com/questions/2114488/how-to-serve-a-file-with-jsp/2114569#2114569 for a code example.
BalusC
@BalusCOK, I'll remove all the markup text from the validation jsp.
Lirik
You're welcome. Don't *you* get more points if you use a servlet acting like a front controller? :) Thus, hide JSP in `/WEB-INF` and forward to JSP as well in `doGet()`.
BalusC
@BalusCThe requirement specifically said to use JSP validation, although I have no problem making a servlet. Is there a benefit to using the servlet for validation rather than the JSP? The JSP gets converted to a servlet anyway...
Lirik
Clean separation of model (javabean), view (JSP) and controller (Servlet). Also, real world's normal practice is that "plain Java code" should go in a real Java class rather than a JSP file. In other words, use of *scriptlets* is discouraged. Use taglibs in JSP to control page flow/output and use EL to access "backend data". But OK, I don't think that your tutor would worry much about that ;)
BalusC