jsp by keralaguest

VIEWS: 42 PAGES: 2

									Distributed Web Systems


                                    Laboratory exercises
                                    Introduction to JSP

Learning goals:

    Practice writing JSP pages
    Learn to use JSP expressions, scriptlets, and JSP directives


Explanations:

Recall the work you did for Lab “Handling HTML forms”. If you missed that lab, just look
up its description on the course web page. In the first part of that lab, you needed to create a
registration HTML form and write a servlet that processes input data from that form.

In this lab, you will need to create exactly the same registration form application. However,
instead of a servlet, you will need to use a JSP for data processing.


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
     directory.

    Copy your “index.html” file from the “Handling HTML forms” Lab into the “web”
     sub-directory of your new project. If you did not do that Lab, create a new
     “index.html” file with the registration form as described in the lab sheet (or download
     a ready one from the course web page).

    Modify your “index.html” so that the form action calls a JSP instead of a servlet (e.g.
     “register.jsp”).

    Write a JSP that performs exactly the same functions that your servlet in the
     “Handling HTML forms” Lab. That is:

           a. Read the input data submitted by the user.
           b. Check that the name field is not empty. If it is empty, you 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, you 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
              registration.

       Hint: you can use JSP scriptlets for conditional data processing (e.g. checking validity
       of the input data).
Distributed Web Systems


    Put this JSP into your “web” sub-directory.

    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. Notice: though you do not have servlets in this
     application, you still need to provide a descriptor. So, all you will need to do is to
     simply remove all servlet definitions and mappings.

    Build and deploy your application using Ant (Notice: there are no .java files to
     compile here, so Ant will simply copy files from your “web” into your “build” sub-
     directory for deployment).

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


In the remainder, let’s practice some JSP directives.

    Write a JSP that displays a random number between 1 and 42 (or what’s the highest
     number in lotto?). The JSP should display only the number, no HTML formatting,
     page headers, etc.

    Put this JSP into your “web” sub-directory.

    Write a JSP to display results of a lotto draw:

           a. Display the date of the draw as today’s date using the Java’s “Date” class.
              Hint: you might need to include some additional Java packages to use this
              class.
           b. 6 lucky numbers (i.e. 6 random numbers, please use some nice HTML
              formatting).
           c. The bonus number (i.e. one more random number).

           Since you already have a JSP that displays a random number, you should reuse it
           in your “lotto” JSP.

    Add these JSPs into your “web” sub-directory.

    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).

    Write down your numbers.

    Now… Are you feeling lucky? Call your “lotto” JSP and see if you are!

    If you have got time left, try figure out what may go wrong with your lotto draw. Can
     you fix it?

    Remember to remove your application when you are done.

								
To top