Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Instant Update


Using an MVC Pattern Across the Web

How does a model like MVC scale up to the Web? There are key differences to consider. The Web is stateless, which complicates the ability of model objects to stay synchronized with all the views (HTML pages) that have been generated based on the data model. Also, the number of distributed views and controllers interacting with the data model can be orders of magnitude larger than in stand-alone, rich-client GUI applications. Separating presentation logic from the data model is just as important for Web applications as it is for any other type of software system, but bringing the MVC model to the Web presents unique challenges that require careful consideration and planning.

In many ways, effective separation of concerns is even more important for Web projects than it is for other types of software development. A Web application's UI is often developed by a team of graphic artists who have little or no knowledge of Java coding and syntax, rather than by a team of professional software engineers, as might be the case for a rich-client GUI. If the work is broken up along clearly defined interfaces so that each team can focus on its core competency, the project will be completed much more quickly. By effectively separating concerns early in the development cycle, project managers can ensure that programmers stay focused on programming, and designers on design.

Separation of concerns was the major driving force behind the development of JSP technology. Most HTML page content available on the Web is actually a combination of "static" template HTML markup along with dynamically generated information that is retrieved and/or updated anew with each and every HTTP request. JSP was devised as a means to separate the Web content (much of which could originate in back-end databases) from the way it's presented to the user (e.g., HTML code).

JSPs resemble HTML pages, except they contain embedded segments of Java code. They are a presentation-centric, declarative way of binding dynamic content with application logic. Because they run inside a Web server and are compiled into servlets prior to execution, JSPs are capable of invoking a range of server-side functionality, including transaction support, database access, state maintenance, security, distribution, and load balancing. However, system architects may want to carefully weigh the costs and benefits of invoking particular types of behavior—particularly database access—directly from a JSP page, rather than using some type of middle-tier abstraction layer.


[click for larger image]

A JSP is responsible for both controller and view responsibilities. The data model is encapsulated inside the Enterprise JavaBean (EJB).

JSPs are a more user-friendly way of managing presentation code than are servlets. Servlets are a programmatic tool and are best suited for low-level application functions that don't require frequent modification. Ultimately, however, JSPs are compiled into servlets and run on the Web server itself. The Java code embedded into the (original) JSP page is compiled "as is" into the resulting servlet. The HTML markup portion of the JSP page is compiled into a series of out.println() statements in the resulting servlet, saving developers the time of manually typing in mindless lines of out.println() statements and allowing graphic design teams to work directly on the template layout in HTML. See the Listing for an example HelloWorld.jsp page.

How does JSP technology relate to the MVC model? So far, it doesn't! What I've been describing is known in the JSP specification as a Model 1 architecture (see graphic). In a Model 1 system, the JSP page alone is responsible for processing the incoming request and replying to the client. The data model is usually represented by an entity Enterprise JavaBean (EJB), or possibly by a session EJB that is acting as a façade for an entity EJB. Model 1 succeeds in separating content from presentation, but can result in a significant amount of Java code being added to JSPs. This in turn can slow down a large project if the JSPs are being created and maintained by a team of graphic designers, as is often the case. Moreover, in Model 1, JSPs are statically linked to one another. This makes Web application functionality less reusable, and can complicate adding and modifying client types, data views, and workflow. Each JSP page is also individually responsible for managing application state and verifying client authentication and security.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.