[ Team LiB ] Previous Section Next Section

20.14 Exercises

Exercise 20-1. Modify the HelloNet servlet so that it behaves like the hello.jsp page. That is, if it is invoked without a name parameter specified, it should prompt the user to enter her name and then displays a greeting. 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 submissions from this form.

Exercise 20-2. Modify the HelloNet 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 RequestDispatcher 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.

Exercise 20-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 administrative 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 initialization parameter from the web.xml file to specify the password for the servlet.

Exercise 20-4. This chapter combines the ListManager web application with various stand-alone examples and then packages them all into a single WAR file. Repackage the ListManager web application by itself, and deploy two independent instances of it on a web server to manage subscriptions to two different mailing lists.

Exercise 20-5. The Controller servlet examines the name under which it was invoked and uses that name to dispatch to an action method defined within the servlet. Modify this class so that each action is defined as a separate object. Define an interface named ServletAction with an action( ) method that accepts servlet request parameter and response arguments and returns a RequestDispatcher, just as the current methods of the Controller servlet do. Then change those methods into implementations of this new interface. Modify the servlet so it maintains a Map of URL action names to ServletAction objects. Initialize the map in the servlet's init( ) method.

Exercise 20-6. Extend the ListManager web application by adding a password-protected administrative interface that displays all subscribers and their preferences. The administrative password should be stored as an initialization parameter, of course.

    [ Team LiB ] Previous Section Next Section