5. Components of Generic Web Application
Client: Generally a browser(user agent) is controlled by a user to operate the Web
application. The client’s functionality can be expanded by installing plug-in sandapplets.
Firewall: A piece of software regulating the communication between insecure networks
(e.g.,the Internet)and secure networks(e.g.,corporate LANs). This communication is
filtered by access rules.
Proxy: A proxy is typically used to temporarily store Web pages in a cache. However
, proxies can also assume other functionalities, e.g., adapting the contents for users
(customization), or user tracking.
Web server :A Web server is a piece of software that supports various Web protocols
like HTTP and HTTPS, etc., to process client requests.
Database server: This server normally supplies an organization’s production data in
structured form ,e.g., in tables.
Media server: This component is primarily used for content streaming of non-
structured bulk data(e.g.,audio or video).
Content management server: Similar to a database server, a content management
server holds contents to serve an application. These contents are normally available in
the form of semi-structured data ,e.g., XML documents.
Application server: An application server holds the functionality required by several
applications, e.g., workflow or customization.
Legacy application: A legacy application is an older system that should be integrated as
an internal or external component.
8. Why J2EE ?
Technical Benefits
• Enables component based approach to develop enterprise web application.
• Reduces cost and complexity of developing large,multi-tier-web-based
application.
• Component types supported,State management,Database caching,declarative
(automated) persistance,scalability through load balancing.
Buisness Benefits
•Indusrty buy-in and choice of vendors,greater utilization of existing IT assets.
• Faster response to change,choice of proven ,scalable and reliable platform.
9. Components of J2EE Web Application
Client Tier
• Client tier supports both applet container and application client container.
• Applet container is the container to manage the execution of applets.Exam-
-ples of an applet container are applet viewer,web browser and java Plug-in
running on the client together.
• Application client container is the one which is responsible for managing the
application client components. Both application client and its container resides
at the client machine and are executed on the client end. Example of
application container is JVM.
10. Components of J2EE Web Application
Middle Tier
• Whole presentation and buisness logic is executed in middle tier which con-
sists of Web container , EJB container and a number of industry standered APIs
for accessing Enterprise Information system and Database.
• EJB Container
Enterprise Java Beans (EJB) is a tecnology in J2EE platform,which standerdizes
how to build server side components for distributed applications.It is a reusa-
ble software components written in java,which run inside EJB Container.EJB
container is a part of J2EE application server which manages whole lifecycle of
EJBs and provide them with some important system level services.When client
sends request through JSP,JSP request EJB for information.Once EJB generate
Information from database,JSP file then display it to client.
EJB is NOT relevant to our discussion to Struts.
11. Components of J2EE Web Application
Middle Tier
• Web Container
In Web Container also known as Servlet container,JSPs and Servlets are main
non-pure java components mixed with plain static pages.They can also
generate dynamic HTML with dynamic contents coming from back end tier and
interact with client-tier components.
In J2EE platform,the presentation between end user and system response
should be handled by JSP and Servlets technologies and buisness logic should
be encapsulated by Java Bean (or Enterprise Java Bean).
•Industry Standerd APIs are for accessing Enterprise Information system and
Database. They include following services : 1)HTTP,HTTPs 2)Java Transaction
API(JTA) 3)Java Interface Definition Language(JavaIDL) 4)Java Naming and
Directory Services (JNDI) 5)Java Message Services (JMS) 6)RMI-IIOP etc.
12. Components of J2EE Web Application
• Java EE Containers
1) Application client container
2) Applet Container
3) Web container – part of Java EE Application Server
4) EJB Container - part of Java EE Application Server
All of the above comes under Java EE Containers.
• Java EE Application Servers
J2EE specification defines a whole lot of interfaces and a few
classes.Implementations for these interfaces adhering to the J2EE
specifications. These implementations are called J2EE Application
Servers.
Example : WebLogic by Oracle,WebSphere by IBM,Gerinomo,TomEE
by Apache,Jboss etc
14. CGI ,SERVLET AND JSP
Evolution of Dynamic Content Generation
When World Wide Web (WWW) started in the year 1989 at he CERN laboratory
the idea was to have a mechanism which enabled sharing of research
information among researchers using hypertext docs. And hence Web was
designed to serve static contents in the beginning. The obvious and natural
progression was to have the ability of dynamic content generation on Web and
there came the concept of CGI.
CGI - Common Gateway Interface
CGI was designed to generate the contents on the Web dynamically. It became
hugely popular in no time as it allowed the Web applications to access the
database to show results based on dynamically selected criterio and it also
facilitated insert/update of information in the database accepting the info from
the end-user.As the popularity grew the scalability of the web applications also
grew and then we started realizing the limitaions of CGI.
15. CGI ,SERVLET AND JSP
Limitations of CGI
New Process per Request - the main drawback of CGI approach was that it
spawned a new heavy-weight Operating System process everytime a new
request was made from the end users (from browser). This immensely
restricted the scalability and responsiveness of the web applications as creating
(and reclaiming once the request is served) OS processes are time and resource
consuming stuff.Communication Gap between Web Server and Requests - since
all the requests are executed in a different OS process from the Web Server
process hence it becomes difficult to have a smooth communication between
the server and the requests to handle stuff like logging, authorization, etc.
Alternatives of CGI FastCGI,mod_perl,NSAPI,ISAPI Most important and
Popular one :Java Servlets
16. CGI ,SERVLET AND JSP
Java Servlets - what are they?
Sun Microsystems introduced this technology in the year 1997 and it became an instant hit
due to various advantages it provided over CGI. It was no longer required to have a new
process every time a request was made from an end-user.
It was a platform-independent (As it is written completely in Java), component-based
approach of developing web applications having dynamic content genration capabilities.
Since it is written in Java hence all the tested and tried rich set of APIs of Java are readily
available to be used and this advantage took this technology way above its competitors.
18. CGI ,SERVLET AND JSP
J2EE Web Application
Any web application that runs in the servlet container is called a J2EE web
application. The servlet container implements the Servlet and JSP specification.
It provides various entry points for handling the request originating from a web
browser. There are three entry points for the browser into the J2EE web
application - Servlet, JSP and Filter. You can create your own Servlets by
extending the javax.servlet.http.HttpServlet class and implementing
the doGet() and doPost() method. You can create JSPs simply by creating a
text file containing JSP markup tags. You can create Filters by implementing the
javax.servlet.Filter interface.
The servlet container becomes aware of Servlets and Filters when they are
declared in a special file called web.xml . A J2EE web application has exactly
One web.xml.
19. CGI ,SERVLET AND JSP
Why were JSPs needed when we had Servlets?
Servlet Drawbacks
Servlets do an excellent job of dynamic content generation, but it becomes difficult and
tedious to use Servlets for presentation of the data in HTML. EVery HTML change requires
us to recompile the Servlet and the maintenance of Servlets become difficult as HTML
changes are quite frequest in nature and using Servlets to do that ends up making
corresponding Java code changes everytime.
Another serious drawback of this approach was that it didn't facilitate clear separation
of roles and responsibilities. HTML design and devlopment is primarily the responsibility
of a Web Designer (usually a person having limited Java expertise) whereas the
responsibility of design & development of Servlets belongs to Java Developers. Using
Servlets for presentaion of data mixed both these roles and hence the entire devlopment
life cycle used to be more complex and hence slower. A clear separation of roles and
responsibilities enhanced the overall development cycle and it also made the applications
better maintainable.
20. CGI ,SERVLET AND JSP
Java Server Pages (JSP) Technology
JSP (JavaServer Pages) Technology is used to achieve the clear separation. A JSP
can use normal HTML tags/elements the way we can have in any other normal
HTML file and in addition it can have Tags, Scriptlets, etc. to encapsulate the
business logic for the dynamic content generation. A Web Designer can simply
use those tags or leave the scriptlets to be embedded by the Java Developers. In
fact, it's become a better practice to avoid scriptlets as much as we can from a
JSP page and to rely only on Tags for the dynamic content generation. This not
only makes the life of JSP Page easier, but also enhances the reusability of the
code (writen for Tags) and hence improves maintanability.
There are two models for developing JSP based web application architectures.
• Model 1 Architecture
• Model 2 Architecture (MVC)
22. Model 1 Architecture – How it Works
In this architecture a JSP Page is used not only for the display of the output to the
client, but also for the entire request processing including Accepting the
Request, Creating JavaBeans (or connecting to the DB for data), Executing the
Business Logic to generate Dynamic Content, etc
Let us illustrate the operation of Model 1 architecture with an example.
Consider a HTML page with a hyperlink to a JSP. When user clicks on the
hyperlink, the JSP is directly invoked. This is shown in Figure 1.1. The servlet
container parses the JSP and executes the resulting Java servlet. The JSP contains
embedded code and tags to access the Model JavaBeans. The Model JavaBeans
contains attributes for holding the HTTP request parameters from the query
string. In addition it contains logic to connect to the middle tier or directly to the
database using JDBC to get the additional data needed to display the page. The
JSP is then rendered as HTML using the data in the Model JavaBeans and other
Helper classes and tags.
23. Model 1 Architecture – Disadvantages
There is some separation between content (Model JavaBeans) and presentation
(JSP). This separation is good enough for smaller applications. Larger applications
have a lot of presentation logic.
The presentation logic usually leads to a significant amount of Java code
embedded in the JSP in the form of scriptlets. This is ugly and maintenance
nightmare even for experienced Java developers.
In large applications, JSPs are developed and maintained by page authors.
The intermingled scriptlets and markup results in unclear definition of roles
and is very problematic.
Application control is decentralized in Model 1 architecture since the next
page to be displayed is determined by the logic embedded in the current
page.Decentralized navigation control can cause headaches. All this leads us
to Model 2 architecture .
32. Model 2 Architecture – How it Works
The main difference between Model 1 and Model 2 is that in Model 2, a
controller handles the user request instead of another JSP. The controller is
implemented as a Servlet. The following steps are executed when the user
submits the request.
1. The Controller Servlet handles the user’s request. (This means the hyperlink
in the JSP should point to the controller servlet).
2 .The Controller Servlet then instantiates appropriate JavaBeans based on the
request parameters (and optionally also based on session attributes).
3 .The Controller Servlet then by itself or through a controller helper
communicates with the middle tier or directly to the database to fetch the
required data.
4 . The Controller sets the resultant JavaBeans (either same or a new one) in
one of the following contexts – request, session or application.
5 . The controller then dispatches the request to the next view based on the
request URL.
6. The View uses the resultant JavaBeans from Step 4 to display data.
33. Model 2 Architecture – How it Works
The main difference between Model 1 and Model 2 is that in Model 2, a
controller handles the user request instead of another JSP. The controller is
implemented as a Servlet. The following steps are executed when the user
submits the request.
1. The Controller Servlet handles the user’s request. (This means the hyperlink
in the JSP should point to the controller servlet).
2 .The Controller Servlet then instantiates appropriate JavaBeans based on the
request parameters (and optionally also based on session attributes).
3 .The Controller Servlet then by itself or through a controller helper
communicates with the middle tier or directly to the database to fetch the
required data.
4 . The Controller sets the resultant JavaBeans (either same or a new one) in
one of the following contexts – request, session or application.
5 . The controller then dispatches the request to the next view based on the
request URL.
6. The View uses the resultant JavaBeans from Step 4 to display data.
34. Model 2 Architecture – Advantages
No presentation logic in JSP, there are no scriptlets. This means
lesser nightmares,although MVC does not architecturally prevent you from
adding scriptlets.
With MVC you can have as many controller servlets in your web application, one
Controller Servlet per module.
A single controller servlet for the web application lets you centralize all the
tasks in a single place,avoids duplication of code.
Web applications based on Model 2 architecture are easier to maintain and
extend since the views do not refer to each other and there is no presenta-
-ion logic in the views.
It also allows you to clearly define the roles and responsibilities in large
projects thus allowing better coordination among team members.
35. Model 2 Architecture – Disadvantages
Controller gone bad – Fat Controller
If MVC is all that great, why do we need Struts after all? The answercomplexities.
the difficulties associated in applying bare bone MVC to real world
lies in
In medium to large applications, centralized control and processing logic in the
servlet – the greatest plus of MVC is also its weakness.
Consider a mediocre application with 15 JSPs. Assume that each page has five
hyperlinks (or five form submissions). The total number of user requests to be
handled in the application is 75. Since we are using MVC framework, a centralized
controller servlet handles every user request. For each type of incoming request
there is “if” block in the doGet method of the controller Servlet to process the
request and dispatch to the next view. For this mediocre application of ours, the
controller Servlet has 75 if blocks. Even if you assume that each if block delegates
the request handling to helper classes it is still no good. You can only imagine how
bad it gets for a complex enterprise web application. So, we have a problem at
hand. The Controller Servlet that started out as the greatest thing next to sliced
bread has gone bad. It has put on a lot of weight to become a Fat Controller.
38. MVC with Configurable Controller
How It works
1. Properties file :It contains information in key value pair which is actually
one to one mapping URL for each http request and handler class. There is a
unique handler class for each url.
2 . Controller Servlet loads the properties file on startup
3 . When the HTTP request arrives from the client, the Controller Servlet looks
up in a properties file to decide on the right Handler class for the HTTP
request.This Handler class is referred to as the Request Handler.
4 . The Request Handler contains the presentation logic for that HTTP request
including business logic invocation. In other words, the Request Handler does
everything that is needed to handle the HTTP request.
5 . The only difference so far from the bare bone MVC is that the controller
servlet looks up in a properties file to instantiate the Handlerinstead of calling
it directly.
39. MVC with Configurable Controller
6. there must be some sort of commonality between the Request Handlers
for the servlet to generically instantiate the Request Handler. The commonal-
-ity is that all Request Handler classes implement a common interface. Let us
call this common interface as Handler Interface. In its simplest form, the
Handler Interface has one method say, execute().
7 . The Controller Servlet instantiates the Request Handler in the doGet()
method and invokes the execute() method on it using Java Reflection. The
execute() method invokes appropriate business logic from the middle tier and
then selects the next view to be presented to the user.
8 . The controller servlet forwards the request to the selected JSP view. All this
happens in the doGet() method of the controller servlet. The doGet() method
lifecycle never changes.What changes is the Request Handler’s execute()
method.
9. You just saw how Struts works in a nutshell!!! Instead of using a properties
file like we did in this example, Struts uses XML to store more useful
information.
42. Struts Framework Components
1. All the core components of Struts framework belong to
Controller category.
2. Struts has no components in the Model category.
3. Struts has only auxiliary components in View category. A
collection of custom tags making it easy to interact with the
controller. The View category is neither the core of Struts
framework nor is it necessary.However it is a helpful library for
using Struts effectively in JSP based rendering.
43. Struts Framework Components
Controller Category
The ActionServlet and the collaborating classes form the controller
and is the core of the framework. The collaborating classes are :
1.RequestProcessor
2.ActionForm
3.Action
4.ActionMapping
5 .Action Forward.
View Category
It contains utility classes – variety of custom tags making it easy
to interact with the controller. It is not mandatory to use these
utility classes. You can replace it with classes of your own.
44. Struts Framework Components
Model Category
Struts does not offer any components in the Model Category.Many
component models (CORBA, EJB) are available to implement the
business tier.
NOTE: Some people argue that ActionForm is the model component.
However ActionForm is really part of the controller. The Struts
documentation also speaks along similar lines. It is just View Data
Transfer Object – a regular JavaBeans that has dependencies on the
Struts classes and used for transferring the data to various classes
within the controller.
45. Struts Request Lifecycle
A request from the client browser creates an HTTP request which comes to the servlet
container.
Step 1 :
Servlet container looks up into web.xml,using load-on-startup Instantiate the
ActionServlet and do following three tasks:
First Task : The ActionServlet takes the Struts Config file name as an init-param.
At startup, in the init() method, the ActionServlet reads the Struts Config file and load
into memory.
46. Struts Request Lifecycle
Second Task : If the user types http://localhost:8080/app/submitForm.do in
the browser URL bar, the URL will be intercepted and processed by the
ActionServlet since the URL has a pattern *.do, with a suffix of "do".
Because servlet-mapping is
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Third Task : Then ActionServlet delegates the request handling to another
class called RequestProcessor by invoking
its process() method.
47. Struts Request Lifecycle
Step 2 :
The RequestProcessor does the following in its process() method
a) The RequestProcessor looks up the configuration file for the URL pattern
/submitForm (if the URL is http://localhost:8080/app/submitForm.do). and
and finds the XML block (ActionMapping).
ActionMapping from struts-config.xml
48. Struts Request Lifecycle
b) The RequestProcessor instantiates the EmpForm and puts it in appropriate
scope – either session or request.The RequestProcessor determines the
appropriate scope by looking at the scope attribute in the same ActionMapping.
c) RequestProcessor iterates through the HTTP request parameters and populates
the EmpForm.
d) the RequestProcessor checks for the validateattribute in the ActionMapping.
If the validate is set to true, the RequestProcessor invokes the validate() method
on the EmpForm instance.
This is the method where you can put all the html form data validations.
If Validate fail the RequestProcessor looks for the input attribute and return to JSP
page mentioned in input tag.
If Validate pass goto next step.
e) The RequestProcessor instantiates the Action class specified in the
ActionMapping (EmpAction) and invokes the execute() method on the EmpAction
instance.
49. Struts Request Lifecycle
Signature of the execute method :
f)Association of the logical name and the physical JSP page is encapsulated in the
ActionForward instance returned from the execute method.
In return mapping.findForward("success") RequestProcessor looks for the success
attribute and forward to JSP page mentioned in success tag. i.e success.jsp.
In return mapping.findForward("failure")
RequestProcessor looks for the failure attribute and forward to JSP page mentioned in
failure tag. i.e. failure.jsp
In J2EE terms, this is referred to as dispatching the view to the user.
50. ‘Simple Hello World’ Example
Requirements :
Softwares
1. Eclipse IDE http://www.eclipse.org/downloads/
2. Tomcat Server http://tomcat.apache.org/download-60.cgi
3. JDK www.oracle.com/technetwork/java/javase/downloads/index.html
Libraries Struts 1.3 FILES
XML Java
1.web.xml 1.HelloWorldAction.java
2.struts-config.xml 2.HelloWorldForm.java
JSP
1.HelloWorld.jsp
2.index.jsp
3.failure.jsp
http://struts.apache.org/download.cgi
60. STEPS TO EXECUTE
1. Create dynamic web project in eclipse
2. Add jsp,xml and java files according to directory structure.
3. Add struts libraries in Web-INF/lib folder.
4. Add same struts libraries in java build path(Right click on main
project ->properties->java build path->libraries ->add external
jar
5. Add tomcat server (Windows menu -> show view ->servers ->OK
6. Go to server wizard -> add new server - >Apache -> select the
tomcat version you downloaded ->Next ->Browse ->select the
folder where you extracted or installed the downloaded
tomcat ->finish
7. After adding server,right click on Project-> Run as -> Run on
Server ->select server you added ->Finish