JSP and Model-View-Controller Paradigm
Many web applications are developed within a conceptual
framework known as the Model-View-Controller (MVC) paradigm, and several JSP
facilities are often used when developing within this framework.
MVC Basics
Most real-world web applications contain a large number
of components (servlets and JSP documents) as well as numerous support files
(such as JavaBeans classes). The Model-View-Controller paradigm is widely used
in such web applications for organizing the components and support files. The
MVC paradigm can be applied in any system that has both data processing and
data presentation requirements.
MVC and Web Applications
A web application following the MVC organizational
paradigm will typically have a single Controller
that receives all incoming HTTP requests. The controller provides a single
point of entry to the application. It performs initialization, logging, and
controls access to the application.
The controller may also interact with Model components of the application.
These are software components that represent the persistent data of the
application and server-side processing performed on this data.
Finally, when the controller and model software have
performed all necessary preprocessing of a request, the controller selects an
appropriate View (presentation)
component and forwards the HTTP request to that component. The view component
will generally obtain data from the request and/or model components and then generate
an HTTP response that presents a formatted view of this data.
In web applications, the controller is often implemented
as one or more Java servlets. The model components will typically consist of
JavaBeans classes and/or a database. View components are generally JSP documents.
MVC and Servlet Request Dispatching
In an
MVC-structured web application,
1.
A single
Java servlet receives every incoming HTTP request intended for the application.
2.
This
servlet processes the request data, typically via interactions with JavaBeans
objects and/or a database.
3.
When
this data processing is completed, the controller servlet will select an appropriate
JSP view component and forward the HTTP request on to that component.
4.
The
selected JSP document then produces an HTTP response based on information in
the forwarded HTTP request as well as information obtained from other model
data sources, such as JavaBeans objects.
5.
A Java
servlet creates a RequestDispatcher object using URL
or Servlet name. If the JSP container cannot find the requested resource, then
the method call returns null
RequestDispatcher
RD = getServletContext().getRequestDispatcher(contextRelativeURL);
RequestDispatcher
RD = getServletContext().getNamedDispatcher(ServletName);
6.
The
Servlet then forwards an HTTP request by calling the forward() method on the RequestDispatcher object with two arguments – the HttpServletRequest and HttpServletResponse
objects
7.
The
component to which the request is forwarded behaves as if it received the HTTP
request directly rather than via the servlet.
The
controller can pass data to a view component in several ways.
1.
It can
store data in a persistent store that is accessible to the component, such as
in a database or file.
2.
It can
call setAttribute() to assign values to attributes of
the HttpServletRequest, HttpSession,
or ServletContext objects
3.
The
controller can add parameters to the HttpServletRequest
object passed via forward() to the component.
Example Program: Servlet acting as MVC
controller in a very simple application
// MVC controller servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Controller extends HttpServlet
{
/* If
session is new then increment and display the application visit counter. Otherwise
(this is the continuation of an active session), display a message. */
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException
{
HttpSession session = req.getSession();
if (session.isNew())
{
RequestDispatcher visitDispatch = getServletContext().getNamedDispatcher("visit_count");
visitDispatch.forward(req, res);
}
else
{
RequestDispatcher laterDispatch = getServletContext().getNamedDispatcher ("visit_later");
laterDispatch.forward(request, response);
}
}
}
The
deployment descriptor for the above application:
<servlet>
<servlet-name>visit_count</servlet-name>
<jsp-file>/HelloCounter.jspx</jsp-file>
</servlet>
JSP Actions Supporting MVC
Sometimes, one JSP view component may wish to call on
other view components in order to generate portions of its output. For example,
a JSP document might be responsible for generating a Web page that has a
navigation bar along with the primary page content. We might like to design the
original JSP document to call on two other JSP documents, one to generate HTML
representing the navigation bar and the other to generate the primary content
of the page. This design makes it easy to incorporate the navigation bar or
content elements in other view components.
The JSP standard action include causes the container to
execute a component of a web application and to append the body of the response
produced by the component to the out implicit object of the JSP document
containing the include action. Other output actions performed by the component,
such as setting header field values, will be ignored. The primary attribute of
include is page, which specifies a URL of the component to be executed. Thus,
if /navbar.jspx and /mainContent.jspx
are the URLs for components generating a navigation
bar and primary page content, respectively, then the following JSP markup will
generate an HTML table containing both of these elements:
<table style="width:100%"
border="0">
<tbody>
<tr>
<td style="width:20%"><jsp:include page="/navbar.jspx"
/></td>
<td style="width:80%"><jsp:include page="/mainContent.jspx"
/></td>
</tr>
</tbody>
</table>
While a controller normally performs forwarding and view
components normally perform inclusion, JSP and the servlet API support other possibilities.
In particular, RequestDispatcher objects have an include() method that takes two arguments, a request and a
response, and performs inclusion type processing. Similarly, JSP supplies a
standard action forward that is analogous to the RequestDispatcher
object’s forward() method described in the previous
subsection. Like the include action, forward has a page attribute, and its
content can optionally consist of param elements.
In
short, the JSP markup
<jsp:forward page="/somewhere.jspx" />
is
essentially translated into the servlet code
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/somewhere.jspx");
if (dispatcher != null) {
dispatcher.forward(request, response);
} else {
// throw run-time exception
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu