Distributed Web Systems

thickbugSoftware and s/w Development

Oct 28, 2013 (3 years and 5 months ago)

64 views

Distributed Web Systems
The Model-View-Controller architecture:
Integrating servlets and JSP
Lecturer
Department
University
Outline

Motivation

Architecture overview

Implementing MVC with servlets and JSP
Motivation

Servlets:

Great when application needs a lot of real programming
to accomplish its task

Can easily customize the output depending on the input
data received

BUT generating HTML in servlets is tedious…

JSPs:

Great for producing HTML (can write it in the normal
manner in JSPs)

BUT provide a single overall presentation

Returning very different pages depending on the input is
possible with scripting, but makes it difficult to maintain
Solution?

Use each technology for what it is good
for!

Servlets – business logic

JSPs – presentation

The
Model-View-Controller architecture
is
the idea that lets you integrate servlets and
JSPs in the desired way


and by the way,
what is architecture?
Model-View-Controller

A bit of history…

A p
opular
generic
design pattern for
many
applications

Defined by
Xerox
in building the Smalltalk-80
system – perhaps the earliest widely-used
object-oriented system

Separate concerns

The model of the data in memory


from the presentation of that data to the user


from the way the user manipulates that data
Model-View-Controller
(contd.)
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
Model
View
Controller
Data and the
allowed operations
Responses to
UI actions
Display of
the data
Communications in MVC
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
Model
View
Controller
View “observes” events
within the model
Control actions invoke
changes in the model
Interactive
controls in
the view
invoke
controller
actions
Changes in the
model cause
“events”
Needs to be a functional
interface to make sure
events are raised correctly
If(x == 5)

{ … }
Else

{ … }
Advantages of MVC

It encourages the
separation of interface from data

Provide a new view and controller for the same data

Provide a new way of controlling the model

Changes in model will propagate to the view, no matter how
they occurred
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
Script
The same model-based
functionality accessed
by a different route
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
MVC for Servlets/JSPs
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
If(x == 5)

{ … }
Else

{ … }
Model
View
Controller
Bea
ns
Beans
Servlets
Servlets
JSPs
JSPs
Implementing MVC

Define beans to represent your application data

Use a servlet to handle requests

Populate beans

invoke business logic to obtain results

Store the beans in the appropriate context

request, session or servlet context

Forward the request to a JSP page

use
RequestDispatcher

Extract data from the beans in JSP and present
them to the user
Populating the beans

Storing data used only in this request:

Storing data used in this requests and subsequent
requests from the same client:

Storing data used in later requests from any client:
ValueObject value = new ValueObject();
request.setAttribute(“key”, value);
ValueObject value = new ValueObject();
HttpSession session = request.getSession();
session.setAttribute(“key”, value);
ValueObject value = new ValueObject();
getServletContext().setAttribute(“key”, value);
Forwarding request to
JSPs
public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

String operation = request.getParameter("operation");

if (operation.equals("operation1")) {

address = “/
WEB-INF
/presentation1.jsp“;

}

else if (operation.equals("operation2")) {

address = “/
WEB-INF
/operations/presentation2.jsp“;

}

RequestDispatcher dispatcher =

request.getRequestDispatcher(address);

dispatcher.forward(request, response);
}
Why is this here???
Forwarding request to
JSPs

If a JSP page only makes sense in the context of
servlet-generated data, we do not want it to be
accessible for user requests directly

WEB-INF
directory is only accessible for servlets

What about forwarding requests to static
pages?

GET requests are Ok

POST – cannot forward them to HTML pages..

Solution:
rename “.html” into “.jsp”
Redirect vs Forward

Recall “
response.sendRedirect
” -- what’s
the difference?

forward:

control is transferred
entirely on the server

sendRedirect:

the server sends a status code (302) back to the
client’s browser –
network traffic is involved!

the client’s browser requests the redirected page
Extracting data from
Beans

Use “
jsp:useBean
…”

BUT

need to make sure JSP does not create the
objects!

use “type” attribute instead of “class”:
<jsp:useBean ... class=“package.Class”/>
<jsp:useBean ...
type
=“package.Class”/>
<jsp:setProperty ...”/>
Extracting data from Beans
(cont.)
ValueObject value = new ValueObject();
request.setAttribute(“key”, value);
ValueObject value = new ValueObject();
HttpSession session = request.getSession();
session.setAttribute(“key”, value);
ValueObject value = new ValueObject();
getServletContext().setAttribute(“key”, value);
<jsp:useBean id=“key” type=“package.ValueObject”

scope=“request”
/>
<jsp:useBean id=“key” type=“package.ValueObject”

scope=“session”
/>
<jsp:useBean id=“key” type=“package.ValueObject”

scope=“application”
/>
Forwarding requests
from

JSP

Not the most common scenario, but still
possible:

Essentially equivalent to:
<jsp:forward page=“Relative URL” />
<% RequestDispatcher dispatcher =

request.getRequestDispatcher(“Relative URL”);

dispatcher.forward(request, response);
%>
Including pages


forward” relies on JSP producing the complete output.
What if we want to combine servlet’s output with one or
multiple JSPs?

Use “include” method:

What’s a common application for this approach?
...
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("...");
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(“/WEB-INF/help.jsp");
dispatcher.include(request, response);
out.println("...");
...
This shouldn’t generate
HTML headers though
Interpreting relative URLs

Keep in mind the specifics of how relative URLs
are interpreted in the destination pages (pages you
forward requests to):
<img src=“
someimage.jpg


alt=“
A picture


border="0">
<img src=“
/path/someimage.jpg


alt=“
A picture


border="0">
Normally, this is relative to
the URL of the page.
However,
when a request is
forwarded to this page, it
will be relative to the URL of
the forwarding servlet!
Solution: always specify the
full path to the resource in
pages to which requests can
be forwarded!
Example request
Servlet
JSP
JavaBeans
Receive
request
Store the
data to be
displayed
Read data
Forward
request
Produce HTML
output with the
given data
Advantages of MVC for
servet/JSP

Outcome:

All internals of data storage are concentrated solely within Java
Beans.

S
ervlet
s
deal only with the application logic, but not with

generating HTML.

JSPs contain no Java code, only HTML and access to the
Beans

Advantages:

W
ant to change the look of our application
--
only need to
modify JSPs, but not our Java code.

W
ant to modify the

way the data are stored
--
only need to
change Beans and nothing else

E
as
y
to maintain and more suitable for

development in larger
teams
!
Why talk about development in teams?
Summary

MVC allows us to
use each technology for what
it is most suitable for

Historically, popular architecture allowing for
separation of concerns
(logic, presentation,
storage) during application development

MVC => Beans – JSP – Servlets

Questions