You are on page 1of 28

JSP Architecture

Outline
 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
Page
 Presents a list of Person objects
 Allows Edit and Create Operations
Person Example Program – Edit
Page
• 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
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"/>
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
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 {
...
redirect(response,"./ShowPeople.jsp");
}
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

 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
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.
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
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"))
{
model.createUser(request.getAttribute("user"),
request.getAttribute("pass"));
}
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
req);
}
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"));
}
}
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 {
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"
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" />
Tag Libraries and M2

Server side
objects
Controller
Dispatch
Client
Action Tag libraries
and business
forward objects
JSP Page JSP page
include