; forms Distributed Web Systems Laboratory exercises
Learning Center
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

forms Distributed Web Systems Laboratory exercises


  • pg 1
									Distributed Web Systems

                                 Laboratory exercises
                       Handling HTML forms and request headers

Learning goals:

    Learn to extract and process data submitted through HTML forms using servlets
    Learn to access and analyse HTTP request headers with servlets


It is assumed in this lab that you are now familiar with the structure of a web application, the
contents of a web application deployment descriptor file, and with using Ant for building and
installing your web applications. If you have difficulties with any of these things, please
revisit the previous lab materials.

You may notice that you have to perform some routine tasks, such as printing out HTML
document headers, over and over in many different servlets. To avoid coding them again
every time, you can put the most frequently used bits of code into a separate helper class and
call them from your servlets. One such helper class called “ServletUtilities” is available on
the course web page. You may want to download it, save together with your other java
sources, and use in your application. For example, you may find its “headWithTitle” method
very useful for printing out HTML page headers.

What to do:

    Create a new Ant project directory for this lab with the “src” and “web” sub-
     directories. Put the Ant build files from the course web page into this project

    Create a simple HTML page with the following

           a. a welcome message;
           b. a registration form with three text input
              fields for user name, password and
              telephone number;
           c. a form submit button. Use an
              appropriate action URL for this form
              (e.g. “Register”). You will map a
              servlet to this URL later on.

           Please see an example in the illustration.
           You are of course encouraged to use fancier
           HTML formatting and layout for a nicer looking page.

    Put this HTML page into the “web” sub-directory and name it as “index.html”, so that
     it becomes the default page for your web application.
Distributed Web Systems

    Write a servlet that performs the following functions:

          a. Read the input data submitted by the user.
          b. Check that the name field is not empty. If it is empty, the servlet should output
             an error page that explains the problem to the user and asks her to repeat.
             Provide a link back to the input form.
          c. Check that the length of the password is at least 8 symbols. If the password is
             too short, the servlet should output an error page that explains the problem to
             the user and asks her to repeat. Provide a link back to the input form.
          d. If the input data are acceptable, output a greeting web page that greets the user
             by her name and also prints out the type of the web browser used for

    Remember to place all your .java sources into the “src” sub-directory, so that Ant
     could compile them automatically.

    Create a web application deployment descriptor for your application and put it into
     “web/WEB-INF”. You can modify the descriptor from the previous lab, or the one
     available on the course web page. The URL pattern for your servlet must match the
     action URL in the form on your “index.html” page.

    Compile and deploy your application using Ant.

    Verify the behaviour of your code for acceptable and incorrect inputs.

Well done! Now you can handle user input from HTML forms and also access information
from the HTTP request headers. In the rest of this lab, you will spend some more time
working with headers.

    Write a servlet that prints out the following information:

          a.   Request method;
          b.   Request URL;
          c.   Request protocol;
          d.   A table displaying names and values for all HTTP headers in this request.

          If your lab time is running out, you may use the ready code for this lab from the
          course web page. However, do spend time in this case familiarising yourself with
          that code.

    Add this servlet to your application descriptor and create an appropriate URL
     mapping for it there.

    Re-deploy your application. That is, do “ant remove” followed by “ant install”.
     Remember that all Ant commands must be issued in your project directory (where the
     Ant build files are).

    Call the servlet and analyse its output.
Distributed Web Systems

    You are done! Remove your application.

To top