Java:Building a Sample Web App with STRUTS

In this article, I will discuss building a sample web application using STRUTS. STRUTS is a leading open-source Web Application Framework. NetBeans is the leading open-source IDE and we will use it to demonstrate how to simplify application development using the IDE. Finally our application will run on TomCat application server.

We will assume that as a reader you are familiar with web application development, specifically using JSP and Java.
We need to install NetBeans IDE (and JDK needed for the IDE). You can install NetBeans from Or click here for simple instructions on how to install the IDE.
Quick overview:
We will create a very simple web application just to demonstrate screen flow and action handling using STRUTS. We will use NetBeans to create the JSP aNDjava files, use tools such as auto generation of getters and setters and finally use the IDE to compile and test the app. NetBeans comes with integrated TomCat.

The application will just have two pages. A Start page where you enter your name and a results page that will display a personalized welcome message.

Ok, so let’s get started.
1. Download the Quick start Zip File from here and extract it to any directory. It should create this directory structure:

/META-INF (Some meta info – ignore for now)

/WEB-INF (Main dir. For our web app –

the TLD files are struts tag libraries. Also there are 2 config files)

/Lib (Struts.jar – the struts framework jar file)

/Src (Here we will keep our sources )
Under this we have dir structure com/openstack/struts/testapp
/Classes (Here we the built classes will be stored)
Note: Normally the struts.jar , tld and config files will be downloaded from Struts website, when you download and install STRUTS. These are provided here just for the ‘quick start’.
2. Start NetBeans and mount this directory(In netbeans, right click on filesystems in editor window -choose the ‘local directory’ option and choose the directory: StrutsNetBeansSample). After you successfully mount the directory the editor window will show the StrutsNetBeansSample directory and underlying WEB-INF/classes directory as two separate entries. If not then mount those directories separately. At this point you can now switch to the ‘project’ window in NetBeans if you prefer that view – the ‘alternate’ view.

3. Now let us create our first JSP – a Test Form. A sample JSP is provided under the quickstart directory. Create the TestForm.jsp under the document base (StrutsNetBeansSample) directory. It should look like this:

4. Let’s take a look at what this file contains: The following are STRUTS tag lib definitions. These tell us we are using the STRUTS tag libraries. Please look at the STRUTS documentation for further details of these tag libraries.

Every html tag that is preceded with ‘HTML’, ‘bean’ or ‘logic’ will be interpreted by the STRUTS framework. For example: < html:html> You can also have normal HTML tags in your JSP that do not need to be interpreted by STRUTS, such as: <P>Welcome < /P> We will use two main aspects of struts–collecting user I/P in a form bean and screen flow using action mapping. For example look at the following form definition. It defines a, text entry box, ipString and a submit button. The form submits to an action called TestAction.

We need to define a javabean for every ‘form’. You could use Dynaform another STRUTS construct but this approach of creating a bean per form is simpler. Struts will automatically copy the values entered by the user into this bean and send it to the ‘action handler’. Hence we will need to define an action handler per action.

5. In this JSP so far we have a text box to enter a ‘string’ and a button to submit the form and an action – ‘TestAction’. Now we need to create a ‘form’ java bean corresponding to the form defined in the HTML. Remember the form has only one text entry box for ipString. Hence the bean needs to provide get and set methods for ipString. To do this create the following file: I have created this under com/openstack/struts/testapp directory under WEB-INF/src in our project, as listed bellow.

Basically we just need to create the fields / parameters that are part of the bean. Such as ipString. We will discuss the ‘action’ string later.

Generate getters and setters: In NetBeans, in the left project window, select , click to navigate to the fields. Select the fields (select multiple fields by using SHIFT key). Then while the fields are selected right click and click on tools – ‘generate R/W property for the field’. This should generate the following code:

6. Let us compile this java file. First let us set a parameter in NetBeans – to set target directory where the java files will be compiled (classes will be stored). Go to tools – options compiler types- external compilation (the external compiler, javac is typically the default compiler. Set the target directory to StrutsNetBeansSample /WEB-INF/classes. Click on the file ( in the explorer window and click build – > compile from main menu. The o/p window should show a message like ‘Finished TestFormBean. Now we have a form bean we need an Action class that will take this form bean – use the ipString entered by user to pass to the next page.

7. Lets create the under src/com/openstack/struts/testapp. Here is the class definition:

As you can see all we are doing is taking the ipString (where user will enter his / her name) and then generating a welcome message. Then we reset the ipString with this complete message and set the bean back in the request object. Finally we forward the control to the ‘success’ handler of this action.
Look at the ‘perform’ method. It returns ‘ActionForward’ that forwards the action to the next page (or handler).

The part ‘return (mapping.findForward(“success”))’ will instruct struts to go to next step.

Let”s look at how we specify this ‘next step’ in struts: Open the struts.config from the WEB-INF directory. Look at the action mapping:

The TestAction (from the TestFormPage.jsp) maps to TestActionHandler class under com.openstack.struts.testapp. In our TestFormPage.JSP we have specified the submit action in the form as “TestAction”. So when the form submits STRUTS will read this file to evaluate ‘TestAction’ and call the perform method in:
com.openstack.struts.testapp.TestActionHandler Class.
Finally the mapping.findForward(“success”)) will forward the request to TestResult.jsp, based on the following line in struts.config

So, in summary STRUTS will do the following tasks:
Copy user entered values into a form bean.
Call the action handler passing the form bean.
Facilitate screen flow using the action mapping.

8. Now the final piece – lets write the TestResult.jsp: Here is the code for that:

Looking at the JSP, it defines a bean instance and uses it to print a bean property ipString:

This prints the changed ipString as part of “ActionHandler”.

9. Now we are ready to test. Click on compile all from the main menu, just to make sure that we have all our java code compiled. Right click on theTestFormPage.jsp and click on Execute. Depending on your settings NetBeans might give some warning like HTTP server could not be started etc. Click OK till it displays a browser window. Soon your page should show up.


As discussed we created a sample web application using STRUTS. While doing that we used NetBeans as an IDE, we utilized some tool features (such as auto generate getters & setters), used the integrated TomCat and the compile & test features of NetBeans.