Java Tutorial - Java Script :
Building a Sample Application with Struts
For this exercise, we will take our example application Old Friends Incorporated and develop the entry screens for the application. This will consist of the entry point and login for the application [index.jsp] as well as the registration screen for those who have yet to register [registration.jsp]. Before we start developing, we need to install the base Struts framework in our Tomcat servlet container. We will be using the latest version as of this writing, Struts 1.1. Installation is very easy: it entails simply copying the /webapps/* .war files from the distribution to the Tomcat TOMCAT_ROOT/webapps directory. The Tomcat servlet container will automatically expand the .war files into
their respective directory structures. The quickest way to start developing with Struts is to shut down your servlet container and copy the struts-blank.war file from your Struts distribution into the TOMCAT_ROOT/webapps directory. Decide on the name for your application and rename the copy of struts-blank.war with that name. For the application we are developing here, we will rename it oldfriends. war. Then, restart the Tomcat servlet container, and it will automatically expand the oldfriends.war into the directory structur TOMCAT_ROOT/webapps/oldfriends/. We can test to see if the template for our Old Friends Incorporated application is correctly installed by invoking it from our browser as follows:
http://localhost:8080/oldfriends
If everything is installed correctly, the page shown in Figure 7.1 should be displayed in the browser.
That was easy! Now that we are confident that we have a good starting point, we will proceed to modify this base application to give us the functionality that is required for our Old Friends Incorporated application.
The entry point of our application will be index.jsp, which will present the user with the screen shown in Figure 7.2.
Users who have already registered previously will log into our application by entering their user ID and password and clicking the Login button. Firsttime users will be able to register with Old Friends Incorporated by clicking
the Register button, and they will be presented with a registration screen. Users have a third option in that they can find out more information about Old Friends Incorporated before registering by clicking the About button.
Our next step will be to create a Form class for this application screen so that we can validate our data prior to passing it to our Action class. Before doing this, we take an accounting of the fields for this screen, and determine that we need to define two fields, UserID and Password. As we mentioned during our earlier discussion of Struts, the Form class is created by subclassing the Struts ActionForm class. Here is the source for our ActionForm class:
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class OldFriendsForm extends ActionForm
{
private String mUserID = null;
