Laboratory exercises Introduction to JSP

mobdescriptiveSoftware and s/w Development

Oct 28, 2013 (3 years and 10 months ago)

64 views

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 appl
ication. 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 pro
ject
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 downl
oad
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. T
hat 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 gr
eeting 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 “w
eb” 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 servlet
s 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 b
etween 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 num
ber).


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 A
nt 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 l
otto draw. Can
you fix it?




Remember to remove your application when you are done.