Understanding Flow of Execution in the Sample Code of Struts Application

To make ActionServlet trapping form page request and to make Action Servlet passing that request to starts Action class the Action url of form page (action attribute value of form tag) should be designed as shown below.

<action path of target Action class>.<extension mode in the url pattern of ActionServlet> 

Example: – if ActionServlet url pattern is *.abc

         If target Action class action path is /xyz then the form page Action url should be “xyz.abc”

Register.html (Form page) (View Layer):

<form action =”reg.do” method =”get”>                                                 —–>3 and reg.do—–>5

Username: <input type =”text” name=”uname”><br>

Password: <input type=”password” name=”password”><br>

<input type =”submit” value =”send”>                                                                 –> 4

</form>

In reg.do

Reg is actionpath of registeraction class”, .do is extension word in the url-pattern of action servlet.

Action Servlet (Controller Servlet)

Server Reads Web.xml/Container                       —->1,2

*.do —> 10

Resultpage.jsp (View Layer)                                —-> 19

Result.jsp

……..

……..

Presentation logic (Response going browser window)          —> 20

RegisterForm.java: (formbeanclass) (controller Layer)

Package app:

Public class Registerform extends action form

{

Private string uname;

Private string password;

Public void set xxx()

{

…..               —-> 14

}

Public void get xxx ()

{

……

}

}

Web-xml (D.D File)

<Web-app>

<servlet>

<s-n>action</s-n>          –> 8

<s-c>org.apache.structs.action.actionservlet</s-c>        —>9

<init-param>

<p-n>config</p-n>

<p-v>|WEB-INF|Structs-config.xml</p-v>

</init-param>

<1-0-s>2</1-0-s>

</servlet>

<servlet-mapping>

<s-n>action</s-n>         –> 7

<u-p>*.do</u-p>

</servlet-mapping>

</web-app>

Struts-config.xml (Strts configuration file and controller layer)

<struts-config>

<form-beans>

<form-bean name=”rf” type=”app.registerform”>         –>13

</form-beans>

<action-mappings>

<action-path =:/reg” type=”app registration” name =”rf”>            —-> reg-11, registeraction-15, rf-12

<forward name=”Success” path=”/result.jsp”/>                               —>18

</action-mappings>

</struts-config> 

RegisterAction (StrutsAction Calss) (Model Layer) 

Package app;

Public class register action extends action

{

Public action forward execute (—-)

{

………………                      –>16

Return actionforward obj (logical success names);            —>17

}

With respect to above sample code:-

1) Programmer deploys struts application in web server / application server.

2)  Based on load-on-startup the servlet container creates ActionServlet class object either during server startup or during the deployment of struts application. In this process ActionServlet reads & verifies the struts-configuration file entries.

3) End user launches the form page (register.html) on browser window.

4)  And 5) After filling up the form end user submits the form page which generates request url “reg.do” (http request)

6), 7), 8), 9) Since Actionservlet url pattern is *.do and form page generated request url having reg.do the ActionServlet traps & takes the request

10) ActionServlet reads form data from the form page

11) Based on “reg” word of request url the ActionServlet searches for an Action class configuration in struts configuration file whose action path is “/reg”

12) The name “rf” attribute of Action tag will be used by ActionServlet to know the form bean i.e linked with struts.Action class

13) ActionServlet creates/locates the register form class object

14) ActionServlet calls set xxx () methods on FormBean class object to write form date to FormBean properties.

15) ActionServlet creates / locates struts Action class object (Register Action)

16) ActionServlet calls execute() method of struts Action class to process the request by executing business logic

17) This execute () method returns ActionForward object with the logical name “success” to ActionServlet.

18) ActionServlet uses ActionForward configuration and got result.jsp as the result page.

19) ActionServlet forwards the control to the result page “result.jsp”.

20) The presentation logic of result.jsp formats the result and sends the results to browser window as response in the form of web page.

Leave a Reply

Your email address will not be published. Required fields are marked *