Frame works like Struts, Spring, Webworks simplifies the development of medum sized to large scale applications. For implementing a small scale project we can use JSP model - 1 architechtrue. A frame work is desinged based on good architecture/desing patterns and provides a better procedures for implementing a project. A frame work contains the code that is commonly required for most of the applications. So when we implement a project based on a frame worr, we end up writing less amount of code. Spring, Struts, and webworks are based on model view controller (MVC) architechture/desing pattern.
Here I'm going to recall servlet concepts with a sample servlet before starting struts
Sugession: Don't use any IDE when you're new to the technology. It will do all the tasks automatically, so you don't have a scope to learn the basics of the technology like how it works, what are all the configurations that we should set inorder to run the API.
public class ActionServlet extends HttpServlet
System.out.println("---Action servlet created---");
public void init() throws ServletException
System.out.println("---Action servlet: init---");
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
System.out.println("--Action servlet: service--");
When the web application is started
1. Web container reads web.xml file
2. As load-on-startup is used for action (name of the servlet) servlet, web container creates a servlet object based on my.own.ActionServlet and invokes init() method.
3. We've used a wild card character in the action servlet url-pattern. When the browser sends a request using the URLs that ends with .hello, web container executes the service() method of action (name of the servlet) servlet.
Struts team has developed an action servlet as we did in the above example and mapped the url pattern *.do (this is the default pattern. If we want we can change of course).
Procedure for setting up a struts based web application
1. Create WEB ROOT directory (eg: create a folder under your current working directory with the name myfirststrutsapp)
2. Copy struts-blank.war to directory created in step 1
3. Move to the directory created in step 1 using change directory command (chdir or cd)
4. Extract the content of the WAR file using the command shown below
jar -xvf struts-blank.war
After executing the above commnad, sturts software components will be extracted into project directory.
Struts software contains
1. A set of tag libraries (struts-html.tld, struts-bean.tld etc.)
2. A servlet class with the name org.apache.struts.action.ActionServlet. This is the controller of struts in MVC pattern.
3. Classes like org.apache.struts.action.Action, org.apache.struts.action.ActionError, etc are provided as part of struts. These classes are used as part of our code.
Struts application startup actions
When a struts based web application started, the following steps will be carried out
1. Web container reads web.xml
2. As load-on-startup is used in web.xml, web container creates a servlet object based on the org.apache.strtus.ActionServlet class provided as part of struts.
3. Web container calls init() method on ActionServlet.
4. The code inside the init() method (i.e.; struts code) of the ActionServlet reads the information available in struts-config.xml
Whenever the browser sends a request using the URL's that ends with .do, web container executes the service() method of ActionServlet.
Action class is a sub class of org.apache.struts.action.Action. As part of struts based applications, we need to provide various action classes.
public class ActionOne extends Action
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception
In order to compile the action classes etc, we must set the class path pointing to the jar files that contains
* Struts related classes and interfaces (struts.jar)
* Servlet related classes and interfaces (weblogic.jar or servlet-api.jar).
The above action class can be used as part of a struts based project by following the steps given below.
1. Copy the action class under WEB-INF/classes directory.
2. Provide the information about the action class using the action tag in struts-config.xml as shown below
<action path="/aone" type="ActionOne" />
After deploying the application, while requesting, we should use .do in the path i.e.; /aone.do in this case. When the browsers sends the request with this path the following steps will be carried out in the container.
1. Web container class the service() method on ActionServlet. i.e.; web container starts the execution of struts code.
2. Struts code drops ".do" from the URL. In this case "/aone" will be left.
3. Struts code checks for the action-mapping for which the path is "/aone" from the action-mappings and the value of type will be taken. In this case type is ActionOne.
4. If ActionOne object is not available, struts creates ActionOne object.
5. Struts calls the execute() method on ActionOne object.
6. As the execute() method returns null, struts code stops processing the request.
org.apache.struts.action.ActionMapping and org.apache.struts.action.ActionForward objects
org.apache.action.ActionMapping object is used to hold the info available in action tags and org.apache.struts.action.ActionForward object is used to hold the info available in the forward tag.
mapping.getPath() - will return the /aone
mapping.getType() - will return ActionOne
mapping.getParameter() - will return the parameter attribute value of action-mapping tag.
mapping.findForward(string) - will return the forward object.