Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Understanding JavaServer Pages Model 2 architecture

Exploring the MVC design pattern

  • Print
  • Feedback

Page 3 of 3

Figure 5: Music Without Borders, checkout view

Notice that all the resources for this application are session aware, since the model here is stored within the session. Consequently, you must ensure that the user does not somehow access the controller directly, even by mistake. You can take care of this by implementing the automatic client redirection to the error page (shown in Listing 6) when the controller detects the absence of a valid session.

About the author

Govind Seshadri is an Enterprise Java Guru for jGuru.com, and the author of Enterprise Java Computing -- Applications and Architecture from Cambridge University Press (1999). Learn more about Govind at jGuru.com. JavaWorld and jGuru have formed a partnership to help the community better understand server-side Java technology. Together, JavaWorld and jGuru are jointly producing articles, free educational Web events, and working together on the JavaWorld bookstore and Web-based training.

Read more about Core Java in JavaWorld's Core Java section.

Listing 6:
error.html
<html>
<body>
<h1>
  Sorry, there was an unrecoverable error! <br>
  Please try <a href="/examples/jsp/shopping/EShop.jsp">again</a>.
</h1>
</body>
</html>


Deploying Music Without Borders

I will assume that you are using the latest version of JavaServer Web Development Kit (JSWDK) from Sun for running the example. If not, see the Resources section to find out where to get it. Assuming that the server is installed in \jswdk-1.0.1, its default location in Microsoft Windows, deploy the Music Without Borders application files as follows:

  • Create shopping directory under \jswdk-1.0.1\examples\jsp
  • Copy EShop.jsp to \jswdk-1.0.1\examples\jsp\shopping
  • Copy Cart.jsp to \jswdk-1.0.1\examples\jsp\shopping
  • Copy Checkout.jsp to \jswdk-1.0.1\examples\jsp\shopping
  • Compile the .java files by typing javac *.java
  • Copy ShoppingServlet.class to \jswdk-1.0.1\webpages\Web-Inf\servlets
  • Create shopping directory under \jswdk-1.0.1\examples\Web-Inf\jsp\beans
  • Copy CD.class to \jswdk-1.0.1\examples\Web-Inf\jsp\beans\shopping
  • Copy error.html to \jswdk-1.0.1\webpages
  • Once your server has been started, you should be able to access the application using http://localhost:8080/examples/jsp/shopping/EShop.jsp as the URL


Leveraging JSP and servlets

In this example, we have examined in detail the level of control and flexibility provided by the Model 2 architecture. In particular, we've seen how the best features of servlets and JSP pages can be exploited to maximize the separation of presentation from content. Properly applied, the Model 2 architecture should result in the concentration of all of the processing logic in the hands of the controller servlet, with the JSP pages responsible only for the view or presentation. However, the downside of using the Model 2 approach is its complexity. Consequently, it may be desirable to use the Model 1 approach for simpler applications.

  • Print
  • Feedback

Resources