Top Banner
JSP Architecture Outline Model 1 Architecture Model 2 Architecture
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 7009801 JSP Architecture

JSP Architecture

Outline Model 1 Architecture Model 2 Architecture

Page 2: 7009801 JSP Architecture

Model 1 architecture

JSP-Centric Architecture

– JavaBeans or EJB Model Objects

– View JSP pages

– Action JSP pages

Page 3: 7009801 JSP Architecture

Model 1 architecture

Page 4: 7009801 JSP 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.

Page 5: 7009801 JSP Architecture

Person Example Program- Navigation Flow

Page 6: 7009801 JSP Architecture

Person Example Program – List Page

Presents a list of Person objects Allows Edit and Create Operations

Page 7: 7009801 JSP Architecture

Person Example Program – Edit Page

• Typical HTML Form Page• Displays Existing Model Object or Empty New Object

Page 8: 7009801 JSP Architecture

Model 1 Architecture - Overview

JSP Components Perform Display and Processing Tasks

Page 9: 7009801 JSP Architecture

Model 1 Architecture – Display Details

• View JSP Pages request model objects from business tier

• Standard JSP Tags used to display data

<jsp:useBean id="person"

class="com.wiley.compBooks.nyberg.ch02.objects.Person" scope="request">

...

person = PersonService.findPersonById(id);

...

</jsp:useBean>

...

<INPUT TYPE="text" NAME="firstName"

value="<jsp:getProperty name="person" property="firstName"/>" size="50"/>

Page 10: 7009801 JSP Architecture

Model 1 Architecture – Form/Submit Details

HTML Form is posted to unique Action JSP page Other techniques include “Self Posting” and “Next-Page

Posting” HTTP Request Parameters extracted using setProperty with *

<jsp:useBean id="person"

class="com.wiley.compBooks.nyberg.ch02.objects.Person" scope="request" />

<jsp:setProperty name="person" property="*" />

Action JSP page performs validation and model updates

Page 11: 7009801 JSP Architecture

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 requestif (errors.size()>0) { %> <jsp:forward page="EditPerson.jsp" /><%} else { ... redirect(response,"./ShowPeople.jsp"); }

Page 12: 7009801 JSP Architecture

Model 1 Architecture – Summary

Benefits of JSP-Centric Approach

Drawbacks of JSP-Centric Approach

Small number of components required to build a given application

Architecture produces a tightly-coupled application with hard coded page names

Small number of technologies, reducing learning curve for inexperienced resources

Action JSP pages are primarily Java code, but cannot be developed, 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

Page 13: 7009801 JSP Architecture

Model 2 architecture

Servlet-Centric Architecture

– JavaBeans or EJB Model Objects

– View JSP pages

– Servlet or Command Classes

Page 14: 7009801 JSP Architecture

Model-View-Controller

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

Page 15: 7009801 JSP Architecture

Features

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 user.

Augments REQUEST (not response) object with additional data to be

displayed to user.

Page 16: 7009801 JSP Architecture

Advantages

MVC approach simplifies JSP pages:

No navigation code inside them.

No complex data manipulation (db access, etc.)

Clean separation of presentation and processing logic.

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

Page 17: 7009801 JSP Architecture

Model 2 Architecture

Page 18: 7009801 JSP 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.

Page 19: 7009801 JSP Architecture

Model 2 Architecture Issues

Single or Multiple controller servlets Different views to be supported Single functionality in page

Page 20: 7009801 JSP Architecture

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

Page 21: 7009801 JSP Architecture

Controller Design

Poorly designed controller

if (op.equals("createUser"))

{

model.createUser(request.getAttribute("user"), request.getAttribute("pass"));

}

else if (op.equals("changeUserInfo")

{

// ... and so on...

}

Page 22: 7009801 JSP Architecture

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 req);

}

Page 23: 7009801 JSP Architecture

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"), req.getAttribute("pass"));

}

}

Page 24: 7009801 JSP Architecture

Controller Design

public class ControllerServlet extends HttpServlet {

private HashMap actions;

public void init() throws ServletException {

actions = new HashMap();

CreateUserAction cua = new CreateUserAction(model);

actions.put(cua.getName(), cua);

}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

Page 25: 7009801 JSP Architecture

Controller Design

String op = getOperation(req.getRequestURL());

Action action = (Action)actions.get(op);

Object result = null;

try {

result = action.perform(req);

} catch (NullPointerException npx) {

}

}

Page 26: 7009801 JSP Architecture

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.

Page 27: 7009801 JSP Architecture

Sample View

<H1>Best Available Flights</H1>

<jsp:useBean id="customer"

class="moreservlets.TravelCustomer"

scope="session" />

Finding flights for

<jsp:getProperty name="customer" property="fullName" />

<P>

<jsp:getProperty name="customer" property="flights" />

<P><BR><HR><BR>

<jsp:getProperty name="customer"

property="frequentFlyerTable" />

Page 28: 7009801 JSP Architecture

Tag Libraries and M2

Client

Controller

Action

JSP Page

Dispatch

forward

Server side objects

Tag libraries and business objects

JSP pageinclude