Thread: Multipage Form

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    Multipage Form


    I have to create a doGet method to handle GET requests and a doPost to handle POST requests using URL parameters. I need to present an HTML form to the user. Ask question 1 and after the user submits the form, process the results and ask an additional question based on their response. (So, I could ask Do you like racing?) After the user selects one, they submit the second form, process the results and print all of the results that I collected along the way. Servlet requires ALL questions to be answered and if they don't, they need to receive an error message. No cookies or session are allowed.

    Here's what I got so far. I'm stuck on getting to the next question.

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;

    public class tg7 extends HttpServlet {

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    showForm(request, out);
    }

    //Shows the Results of the form
    public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    showResults(request, response, out);
    }

    //Print form for user
    public void showForm(HttpServletRequest request,
    PrintWriter out) {
    out.println ("<body bgcolor=6699CC font color=FFFFFF>" +
    "<font size=6>WELCOME TO TERRIS NASCAR SURVEY</b></font></body>" +
    "<form name='tg7' ACTION='http://localhost:8080/examples/servlet/tg7' METHOD='POST'>" +

    "<BR bgcolor=C0C0C0 align=left>What is your first name?<BR>" +
    "<BR width=450 bgcolor=C0C0C0><input type='text' type=hidden name='Hi' size=20 value=''><BR>" +
    "<p><input type='submit' value='Next'></p>");
    }

    //Shows the results of the form or calls the Show Error function if a value is not entered
    public void showResults( HttpServletRequest request,
    HttpServletResponse response, PrintWriter out )
    throws IOException, ServletException {

    response.setContentType("text/html");
    String title ="Here's your NASCAR survey responses";

    Enumeration paramNames = request.getParameterNames();

    while(paramNames.hasMoreElements()) {

    String paramName = (String)paramNames.nextElement();

    out.print("<TR><TD>" + paramName + "\n<TD>");

    String[] paramValues = request.getParameterValues(paramName);

    if (paramValues.length == 1) {
    String paramValue = paramValues[0];
    if (paramValue.length() == 0) {
    showError(request, response, out);
    break; }

    else

    out.println(paramValue);
    }
    else {

    out.println("<UL>");

    for(int i=0; i<paramValues.length; i++) {

    out.println("<LI>" + paramValues[i]);
    }
    out.println("<UL>");
    }
    }
    out.println("</TABLE>\n</BODY></HTML>");
    }

    //shows an error message and re-displays the original form
    public void showError(HttpServletRequest request,
    HttpServletResponse response, PrintWriter out) {

    String title = "STOP!";
    out.println("<BODY BGCOLOR=\"#6699CC\">\n" +
    "<H2 ALIGN=CENTER>" + title + "</H2>\n" +

    "<font size=4>Please go back and enter your first name.</font> <BR>" +

    //link back to assignment 7
    "<p>Go Back to " +
    "<a href=\"http://localhost:8080/examples/servlet/tg7\">" +
    "<B>Terris NASCAR Survey</B></a>" +
    "</p>" +
    "</body></html>");
    }
    }

    So, once they enter their name, the next question would be "Do you like Ford or Chevy? Then depending which answer they pick, they go to either the Ford or Chevy Drivers. Then once they submit that, they get a results type of page.

    HELP!
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    First off, let me bitch that not using sessions for this is rather stupid. I'm guessing you're not the one that's set that as a requirement, though .

    Now, the first thing you're going to need is a control variable so that you know what page you're on. Call it "page", or something. Then, in your showForm() method you can do something like:
    Code:
    if ( request.getParameter( "page" ) == null ) {
       // show page 1
    } else if ( request.getParameter( "page" ).equals( "1" ) ) {
       // show page 2
    }
    Next, familiarize yourself with hidden form fields. Every parameter that comes through from page one needs to be output as a hidden form fields so that it gets submitted to page 2. Every form field from page 2 needs to get output as a hidden form field so that it gets sent to page 3, etc. This way, when you get to the last form page, you'll have all the parameters from the previous pages as hidden fields, plus the last few fields that you're displaying.

    Good luck .
    -james
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    Thumbs up You're Right!


    James,

    First, let me thank you for your help. Second, you're right, it is a requirement of my assignment.

    With what you gave me, I think I got if figured out.

    Thanks for the response.

IMN logo majestic logo threadwatch logo seochat tools logo