Web application development was once simple, there was HTML and a browser. There was no dynamic or business data, so no business applications. But this simplicity lasted for a small while. Today we are forced to do many complicated application developments.

This tutorial assumes that you have the knowledge of JSP, Servlet, custom tag libraries and XML

What is a struts?

Struts from the Jakarta project is a development framework for java servlet application based upon the model-view-controller (MVC). The aim of MVC architecture is to separate the business logic and data of the application from the presentation of data to the user. Following is the small description of each of the components in MVC architecture:


The model represents the data of an application. Anything that an application will persist becomes a part of model. The model also defines the way of accessing this data ( the business logic of application) for manipulation. It knows nothing about the way the data will be displayed by the application. It just provides service to access the data and modify it.


The view represents the presentation of the application. The view queries the model for its content and renders it. The way the model will be rendered is defined by the view.The view is not dependent on data or application logic changes and remains same even if the business logic undergoes modification.


All the user requests to the application go through the controller. The controller intercepts the requests from view and passes it to the model for appropriate action. Based on the result of the action on data, the controller directs the user to the subsequent view


Struts is comprised of controller servelet, beans and others java classes, configuration files, and tag libraries. This means when you download struts you will have the following:

A controller for your application

A collection of java bans and other helper classes that you use in the model part of your application.

A collection of tag libraries used in your jsp-pages

With all these together Struts uses a set of configuration files. Together this gives you the skeleton that you can use to struts to your application. A strut has been designed to give you modularity and loses coupling in your application. If yours building a simple, small application you might find it complicated to have to create and handle so may files. You might even be tempted to put all together to put all your code in a single jsp-file. But my advice is “don’t do it”. I know you can build single page application faster using only one jsp page. But if were talking ballot more complex application, the extra effort put in by using modular framework will soon be rewarded

Downloading and installing struts

Struts is downloaded form the JAKARATA PROJECT and the simplest way to install struts is to copy a small struts application, packed in struts-blank.war, form the downloaded file to your servlet container. if you are using TOMCAT and many other servlet containers you place the war file in the “webapps” folder and you now only need to restart your server and you will have struts-blank running. On tomcat you can check your installation with this URL: http://localhost:8080/struts-blank/index.jsp

Our first struts application

Our first small application will be a page with an HTML form containing all the well known controls. This is the form below with struts application. The example code is shown below

freewebschools register page

<form method=”post”>
Student name
<input type=”text” name=”name”><br>
<input type=”text” name=”name1”><br>
Sex:  <input type=radio name=male>male
<input type=radio name=female>female<br>
<input type=submit value=submit name=submit>


When we submit this page it should redirect itself without losing the data that we have entered.

Starting a new struts application

The web.xml file is where servlets and other stuff are defined to the servlet container.

<web app>




The file contains 3 sections:

A the definition of the struts servlet named "actionservlet"

The url mapping for the calls to this servlet

The definitions of the struts tag libraries

You will see that the servlet will be called if our browser requests a file when we submit the form in our one page application we will decide to use the action name ""

The mapping of the request to a specific action and action form class is done in the struts-config.file. I have edited the file form “struts-blank” to suit our one page application

<form bean name=”submitform” type=”Freewebschools.playground.Submitform” />
<action path=”/submit” type=”Freewebschools.playgroung.SubmitAaction” name=”submitform” input=”/submit.jsp” scope=”request”.
<forward name=”success” path=”/submit.jsp”/>
<forward name=”failure” path=”/submit.jsp”/>

As you see the file contains two section: the form-beans section, that lists the actionForm beans and the action-mappings.

Building the jsp page

In order for the ActionForm bean to work, you must use struts own tags for creating the HTML form and the controls in it

<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<head><title>freewebschools students</title></head>
<h3> freewebschools students</h3>
<html:form action=””>
student name:<html:text property=”name”/><br>
address :<html:text property=”address”/><br>
sex <html:radio property=”sex” value=”m”/>male

<html:radio property=”sex” value=”f”/>female<br>

Coding the actionform class

This class must extend the struts actionForm, and must have setters and getters for all the form controls in the jsp-page.

Package Freewebschools.playground;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
public final class submitform extends ActionForm
/*student name*/
private String name=”frewebschools”;
public String getLastName(){
public void setLastNmae(String name)
private String address=null;
public String getAaddress
public void setAddress(String address)
private String sex=null;
public String getSex()
public void setsex(String sex)

This class is placed in the WEB-INF/classes/Freewebschools/playground directory

Coding the action class

The action class is seen form the application programmers prospective the heart of the application. This is where you must decide how you will separate your own application code. most often the action class should be kept as thin as possible, placing business logic in other beans or even EJBS’S on other servers.

The implementation of the action class must contain a “perform” method, which receives the request and response objects, the instance and the action mapping information from the configuration file. A very simple ActionForm class, which simply lets the request pass unaltered, is given as following

Import javax.servlet.http.*;
Import org.apache.struts.action.*;
Public final c;las SubmitAction extends Action
public ActionForwared perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
SubmitForm f=(Submit Form) form;
String name=f.getLastName();

This class is also placed in WEB-INF/classes/Freewebschools/playground directory

An important thing to remember is that struts only creates a single instance of the action class, shared amongst all users of the application.

Testing your application

Restart your servlet container

To test your example enter this url in the browser

If you application works, you will see that last name is filled with “Freewebschools”(which means that struts has already created an instance of the actionForm bean, and extracted the data form it)
Now enter all the data in the controls and press submit. Note that url changes to and that all the data stays unchanged in the form

I am able to see the in the url. However, the page itself is coming blank after the submit button.

This is the log msg,

[ActionServlet] Loading chain catalog from jar:file:/C:/jboss-4.0.4.GA/server/default/tmp/deploy/tmp9910Practice-exp.war/WEB-INF/lib/struts-core-1.3.8.jar!/org/apache/struts/chain/chain-config.xml
22:59:51,876 INFO [ComposableRequestProcessor] Initializing composable request processor for module prefix ''
22:59:51,913 INFO [CreateAction] Initialize action of type: org.biswa.orem.ut.SubmitAction

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.