Java Reference
In-Depth Information
Exercises
18-1. Modify the
Hello
servlet so that it never offers a generic “Hello World”
greeting. Instead, it should either greet the user by name or ask the user for
a name, store that name in the session, and then greet the user by name.
Although you are writing a single servlet, it should be able to display two
distinct “pages” of output: a greeting page and a login page. The login page
displays an HTML form, and the servlet should be able to handle submis-
sions from this form. Use the HTML techniques shown in the
DecorBox
class
to decorate the login page.
18-2. Modify the
Hello
servlet again, as in the last exercise. This time, however,
do not hardcode the HTML for the greeting and login pages in the servlet
itself. Instead, implement the contents of these pages in JSP files, and use
the servlet as the controller that processes the input and decides when each
page should be displayed. Your servlet class should use a
RequestDis-
patcher
to forward the request to the appropriate JSP page for display. It
can use the session object or request attributes to pass data from the servlet
to the JSP pages.
You may want to look at the
login.jsp
example for inspiration on the design
of a login page. Note, however, that
login.jsp
displays the login page and
also processes the results of submitting the form it displays. In this exercise,
all the form-processing logic should be in the servlet itself, and the JSP
pages should be pure presentation, containing as little Java code as possible.
18-3. Modify the
Counter
servlet to use a database rather than a local file as its
persistence mechanism. Write a
CounterAdmin
servlet that is an administra-
tive interface for the
Counter
servlet; it should display (but not update) each
of the counts stored in the database. The
CounterAdmin
servlet should be
password-protected and should display a JSP-based login page that requires
the user to log in before the current counts are displayed. Use a servlet ini-
tialization parameter from the
web.xml
file to specify the password for the
servlet.
18-4. The
login.jsp
and
portal.jsp
examples in this chapter demonstrate a number
of ways JSP can be used: they show how Java code can be embedded
directly within JSP and also how JavaBeans can be used with JSP. Another
technique for using JSP that is becoming quite popular is the Model-View-
Controller, or MVC, architecture. In this architecture, JavaBeans or other Java
objects contain the relevant data; they are the “model.” JSP pages provide a
“view” of the data but contain very little Java code, so that they can be cre-
ated and edited by graphic designers instead of by Java programmers. A
web application may contain a number of different JSP pages to provide var-
ious views of the data. Finally, a central servlet acts as the “controller,”
deciding what view to display in response to each request and using a
RequestDispatcher
to forward the request to the appropriate JSP page.
For this exercise, redesign the
portal.jsp
example to use the MVC architec-
ture. Write a single
Portal
servlet as the controller and define at least three
JSP views: a login page, a main content page of the portal, and a page that