Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

*** Limitations of developing web-application using JSP/Servlets

1) Manually retrive Request Parameters


2) Developer has to write validation code manually
3) Validation code is mixed with business logic
4) We cannot publish the webappln in multiple languages

solution->Framework (semideveloped or ready code which can be customized )


build on the common ground to provide starting point
benefits:
(i) we achieve RAD (Rapid Application Development) because we dont have to develop appln
from stratch

(ii) Reduces complexity & programming efforts

(iii) Highly vendor & application vendor independent


____________________________________________________

*** STRUTS
- It is a web-application framework based on MVC Design Pattern
- MVC is basically a layered architecture used to seperate business
logic,presentation layer and the flow control
benefits
(i) Easy maintenance because of low coupling between layers
(ii) change in one layer does'nt affect other
(iii) Modularity hence seperation of responsibilities

It consists of the following components


M stands for Model(Business Logic)
-It is responsible for processing code(db access,calculations) independent of
how it is presented
- It encapsulates data + operations
eg: JavaBean/EJB/WebService

V stands for View(Presentation Logic)


It acts as a UI(accept user input & show output)
eg:Html/JSP/Swing/AWT

C stands for Controller(Execution Logic)


-It controls the flow of the application
-It is the communication link between Model & View
eg: servlet

Controller
User Data

End View
Database
Model
User
Display Result

*** Evolution of MVC (Implementation of MVC )

1) MVC Model-1 Design Pattern(Page-Centric Approach)

Here the user request is directly handled by the JSP.The JSP accepts the requests,
retrives data & forwards the data to a Java class(Model) for processing.The result
returned by the model is evaluted in the same JSP and further decision of
forwarding the control to the corresponding component is taken.

Web JSP Database


Java Bean
Browser

<< View + Controller >> <<Model>>


Limitations:
(i) No clear cut demarcation (separation of responsibilities)
(ii) Multiple Controllers(No single point of administration)
(iii) changes in navigation requires changing pages
(iv) Pages are large & complicated
(v) lots of scriplet leads to lesser readability

2) MVC Model-2 Design Pattern(Servlet-Centric Approach)


- It introduced a controller servlet between browser & JSP
- The controller centralizes logic for navigation flow
-The controller receives all the requests then delegatse the business processing to
the Model & then based on returned value by Model sends JSP as response

<<View>> <<Controller>>

Login.jsp LoginBean
Web Servlet
Browser

<<controller>> Register.jsp RegisterBean


main.jsp BalanceBean

Limitations
(i) Navigation flow is hard coded in the controller servlet
(solution:we can externalize the navigation flow in a configutaion file)

(ii) Controller servlet is manually written from scratch

3) Web-Application Frameworks
-STRUTS
-Spring
-JSF(Java Server Faces)

__________________________________________________________

** STRUTS
-is a web-application framework based on MVC Model-2 Design
Pattern

-developed by Apache Software foundation by Mc Clananhan in 2002

-open source(framework can be downloaded for free from website


http://www.apache.org
(free download->struts2->download jar & xml files)

-It provides ready-made controller which is a Filter (not servlet)


named FilterDispatcher
Why Filter?
1) Filter can filter response but servlet cannot (In case of servlet,it transfers
control to next component but never comes back to the servlet)
2) Servlet is loaded only when it receives first request but filter is by default
loaded even before any request(when the application is deployed)
3) Generally filters are used at boundary line not servlets

_________________________________________________________
*** Struts Application Flow

struts.xml

request
Web
Filter Action
Browser Dispatcher Database
<< Model >>
response <<controller>>

JSP

<<view>>
Explanation
1) Web browser sends a request which is bypassed by FilterDispatcher
2) Controller refers to the struts.xml & based on request url delegates request
processing to the appropriate Action class
3) Object of Action is created & Request parameters are populated
Within the Action properties,if reqd converted to the appropriate type
4) Action class does the processing in execute method.If reqd accesses db &
returns result back to the controller
5) Controller refers to the struts.xml & based on returned result sends response
back to the web-browser

**The result is normally a JSP,but it can also be another resource like a PDF file,an Excel sheet or
Media Player…
**The request processing method is a callback method
Syntax: public String execute(){
//Business logic
}
*** steps for developing Struts Application
1)open eclipse->select JavaEE perspective
2)configure the tomcat server & start it
3)create a new dynamic web-application(select 2.5)
4)copy struts JAR files in lib
5)configure the FilterDispatcher in web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6)create JSP pages & Action classes
7)do the mapping in struts.xml(src)
8)Run as on Server
_________________________________________________________

** Structure of a Struts Application

Web Application

src
Action.java
struts.xml
struts.dtd

Web Content

WEB-INF
web.xml

lib struts JAR files

JSP Pages

**struts2 works on the principle of zero configuration(some default values are assumed)Here
the name of the configuration file is not specified (assumed to be struts.xml)

**Declarative Architecture refers to the configuration that does not need explicit definition.It
can be done in 2 ways
1)By using struts.xml
2)By using annotations directly in the action source files
Both methods do the basic job of linking the views to actions

**If Action class doesnot have execute() method then NoSuchMethodException is thrown
**To give more flexibility you can have user defined business method (instead of execute) using
following mappimg in struts.xml
<action class="com.myapp.MyAction" method="mymethod">
</action>

You might also like