You are on page 1of 28

JSP Architecture

 Model 1 Architecture
 Model 2 Architecture
Model 1 architecture

 JSP-Centric Architecture
– JavaBeans or EJB Model Objects
– View JSP pages
– Action JSP pages
Model 1 architecture
Model 1 architecture

 In the Model 1 architecture, the incoming request

from a web browser is sent directly to the JSP page,
which is responsible for processing it and replying
back to the client.
 There is still separation of presentation from content,
because all data access is performed using beans.
Person Example Program-
Navigation Flow
Person Example Program – List
 Presents a list of Person objects
 Allows Edit and Create Operations
Person Example Program – Edit
• Typical HTML Form Page
• Displays Existing Model Object or Empty New Object
Model 1 Architecture - Overview

 JSP Components Perform Display and Processing Tasks

Model 1 Architecture – Display
• View JSP Pages request model objects from business tier
• Standard JSP Tags used to display data
<jsp:useBean id="person"
person = PersonService.findPersonById(id);
<INPUT TYPE="text" NAME="firstName"
value="<jsp:getProperty name="person"
property="firstName"/>" size="50"/>
Model 1 Architecture –
Form/Submit Details
 HTML Form is posted to unique Action JSP page
 Other techniques include “Self Posting” and “Next-Page
 HTTP Request Parameters extracted using setProperty with *
<jsp:useBean id="person"
scope="request" />
<jsp:setProperty name="person" property="*" />
 Action JSP page performs validation and model updates
Model 1 Architecture –
Navigation Details
 View JSP Pages hardcode links to other View pages
 Action JSP Pages hardcode links to destination View pages

// Return to form page if errors are encountered, leaving Person

// on request
if (errors.size()>0) {
<jsp:forward page="EditPerson.jsp" />
} else {
Model 1 Architecture – Summary

Benefits of JSP-Centric Drawbacks of JSP-Centric

Approach Approach
Small number of components Architecture produces a tightly-
required to build a given coupled application with hard coded
application page names
Small number of technologies, Action JSP pages are primarily Java
reducing learning curve for code, but cannot be developed,
inexperienced resources compiled, and debugged as easily as
pure Java code

Re-use of processing and validation

logic is hampered by its placement in
form-specific action JSP pages
Model 2 architecture

 Servlet-Centric Architecture
– JavaBeans or EJB Model Objects
– View JSP pages
– Servlet or Command Classes

 Model-View-Controller approach came from

SmallTalk community
- Involved notification/event models, direct
manipulation of model objects
 MVC today is basically a Tiered architecture
- Interposes Controller components between
View and Model components
- Controller responsible for navigation,
presentation-tier logic, validation
- Emphasizes separation of presentation logic
and model objects

 Clients do not request pages directly.

 All clients requests go to a controller servlet.
 Each request includes data:
The requested action
Any parameters for that action.
 Controller servlet:
Decides which page should be returned to
Augments REQUEST (not response)
object with additional data to be displayed
to user.

 MVC approach simplifies JSP pages:

No navigation code inside them.
No complex data manipulation (db access,
 Clean separation of presentation and processing
 The front components present a single point of entry
into the application, thus making the management of
application state, security, and presentation uniform
and easier to maintain.
 Multiple views using the same model
Model 2 Architecture
Model 2 Architecture

 The processing is divided between presentation (JSPs)

and front components (controllers).
 Presentation components are JSP pages that generate
the HTML/XML response that determines the user
interface when rendered by the browser.
 Front components do not handle any presentation
issues, but rather, process all the HTTP requests. They
are responsible for creating any beans or objects used
by the presentation components, as well as deciding,
depending on the user's actions, which presentation
component to forward the request to.
 Front components can be implemented as either a
servlet or JSP page.
Model 2 Architecture Issues

 Single or Multiple controller servlets

 Different views to be supported
 Single functionality in page
Controller Responsibilities

 Request processing
 Creation of any beans or objects used by the
presentation JSP
 Deciding, depending on the user's actions, which
JSP to forward the request to.
 Data validation
Controller Design

Poorly designed controller

if (op.equals("createUser"))
else if (op.equals("changeUserInfo")
// ... and so on...
Controller Design

Controller using Command Pattern

public abstract class Action

protected Model model;
public Action(Model model) { this.model = model; }
public abstract String getName();
public abstract Object perform(HttpServletRequest
Controller Design

public class CreateUserAction extends Action

public CreateUserAction(Model model) { super(model);}
public String getName() { return "createUser"; }
public Object perform(HttpServletRequest req)
return model.createUser(req.getAttribute("user"),
Controller Design

public class ControllerServlet extends HttpServlet {

private HashMap actions;
public void init() throws ServletException {
actions = new HashMap();
CreateUserAction cua = new
actions.put(cua.getName(), cua);
public void doPost(HttpServletRequest req,
HttpServletResponse resp) throws IOException,
ServletException {
Controller Design

String op = getOperation(req.getRequestURL());
Action action = (Action)actions.get(op);
Object result = null;
try {
result = action.perform(req);
} catch (NullPointerException npx) {
View Responsibilities

 There is no processing logic within the presentation

JSP itself: it is simply responsible for retrieving any
objects or beans that may have been previously
created by the Servlet, and extracting the dynamic
content for insertion within static templates.
Sample View

<H1>Best Available Flights</H1>

<jsp:useBean id="customer"
scope="session" />
Finding flights for
<jsp:getProperty name="customer" property="fullName" />
<jsp:getProperty name="customer" property="flights" />
<jsp:getProperty name="customer"
property="frequentFlyerTable" />
Tag Libraries and M2

Server side
Action Tag libraries
and business
forward objects
JSP Page JSP page