|
Put Struts' HTML Tag Library to Work
See the HTML tag library in action as you use it in a Login application.
by Budi Kurniawan
Posted January 28, 2003
In Part 1 of this article series, you learned how to configure the Struts HTML tag library and the independent tags. In Part 2, you learned the form-related tags. Here in Part 3, you'll see the HTML tag library used in a Login application. You can download the application here. If you read the six-part Struts basics tutorial, you'll notice that this Login application is a gradual enhancement of the Login applications discussed in that series.
The Login application is a simple Struts application that will show how to use the HTML tag library, especially the form-related tags and the tag for error handling. The directory structure is shown in Figure 1.
This article explains the relationship among those classes and their roles in the application. Note that you must copy all the library files to the WEB-INF/lib directory yourself; they are excluded from the downloadable ZIP file to cut download time.
The View
Three JSP pages serve as the view in the MVC paradigm: login.jsp, mainMenu.jsp, and viewSecret.jsp. The login.jsp file contains a form for the user to log in, mainMenu.jsp displays two links (to log out and to view the company's secret), and viewSecret.jsp shows a secret message.
The application's first page is a Login page containing a form with two input boxes: userName and password (see Figure 2). The user is challenged to enter the correct values for both input boxes. On a successful login, the application displays the mainMenu.jsp page, which contains two links: one to log out and one to view the company's secret account number (see Figure 3).
If the logout link is clicked on, the application logs the user out and redisplays the Login page. If the view-secret link is clicked on, the viewSecret.jsp page is displayed. If the login fails, the user is redirected back to the Login page to log in again.
If the user enters the correct username and password, a session object is created for that user and an attribute called loggedIn is added to the session object. The main page and the company secret page can be viewed only if the application can find the loggedIn attribute in the user's session object. So, absence of this attribute or unavailability of a session object will force the user to log in. For this application, use "john" and "123" as the username and password for a successful login. These values have been hard-coded so you don't have to connect to a database or use other storage.
Back to top
|