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

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.