Professional Documents
Culture Documents
1
Unnat-e 10/14/08
Agenda
Servlet BackGround
– Servlets, JSP, web.xml
– Request processing flow , issues with Servlets
StrutsComponents
HelloWorld Application
Exercise
2
Unnat-e 10/14/08
Servlets
Servlet flow of execution
3
Unnat-e 10/14/08
JSP
JSP flow of execution
4
Unnat-e 10/14/08
Issues with JSP and servlets
Real issues
– Servlets embed html into code
– JSP embeds code into GUI
– Project time increases
– Not modular
– Reuse is difficult
– Server side programmers are not comfortable with
GUI code and vice versa
– If target of GUI is changed, server side code gets
affected.
5
Unnat-e 10/14/08
Struts Overview
MVC architecture for Web based
applications
Separates view (JSP) from model (java
code).
6
Unnat-e 10/14/08
Struts overview (Request flow)
7
Unnat-e 10/14/08
Struts Components -
ActionServlet
ActionServlet
– This is the main struts class. It is a servlet. An
application that uses struts directs all the
requests to the ActionServlet class by
specifying a url mapping in the web.xml file.
8
Unnat-e 10/14/08
Struts Components – struts-
config.xml
Similar to web.xml, this is specified as a
parameter in web.xml
It contains two broad categories of
information
– Configuration information for struts
– Action-mappings
Struts-config entries
– Form beans, action-mappings
9
Unnat-e 10/14/08
Struts Components – Action-
Mappings
An action mapping entry in the struts
config file maps a url to a model and a
view.
The model is an action class
The view is a jsp file
10
Unnat-e 10/14/08
Struts Components – Action
Classes
Action classes populate the model when a
request is made – for example if a request is
made to view the details of an employee with a
particular id, then the model will retrieve the
details of the employee using the id.
Action classes should be written by the
application programmer
Action classes should extend
org.apache.struts.action.Action
11
Unnat-e 10/14/08
Struts Components – Action
classes
Action classes need to override the
following method
Public ActionForward execute
Takes following parameters
– ActionMapping mapping
– ActionForm form
– HTTPRequest request
– HTTPResponse response
12
Unnat-e 10/14/08
Struts Components –
ActionForm
Action classes need to access to form variables.
In servlets, this is done via
request.getParameter()
Struts allows the programmer to define a bean
called an ActionForm object. This bean should
contain variables with the same name as the
variables in the form.
An entry should be made in the form-beans
section of struts-config.xml for this class
An entry should be made in the action mapping
for this url
13
Unnat-e 10/14/08
Struts Components –
ActionForm population
mechanism
When a request arrives, struts checks the struts
config.xml and retrieves the action-mapping
corresponding to the url.
It checks if there is a name parameter
corresponding to the action mapping
If there is a name parameter, struts finds out the
corresponding class name from the form bean
entry
It creates an object of this type.
It now traverses via all the members of this
object via reflection, looks up corresponding
variables from the request and populates the form 14
object.
Unnat-e 10/14/08
Struts Components – request
flow details
Tomcat forwards request to ActionServlet
ActionServlet retrieves action-mapping from
struts-config.xml
ActionServlet creates Actionform object
ActionServlet creates Action object
ActionServlet invokes execute on Action
Action performs action directly or by delegating
to business logic
Action creates model and stores it into session
Action returns a Forward
Action Servlet retrieves JSP corresponding to
15
Forward and forwards to the jsp
Unnat-e 10/14/08
Struts Components – JSP tag
libs
Strutsprovides a rich set of tag libraries
which can be used in jsp pages
HTML tag libs can be used to create jsp
forms rapidly
Logic tag libs are used to do evaluation of
expressions and decision making
Bean tag libs are used to manipulate java
beans, cookies, and http headers
16
Unnat-e 10/14/08
The first struts Application
Application shows a small form with a
message
Enter Name <textbox>
<submit> button
On submitting, it should show a screen
with hardcoded details of username.
17
Unnat-e 10/14/08