Search |
||
Comparing webapp frameworks : Model 1 with scriptletsPosted by simongbrown on November 23, 2005 at 11:42 AM PST
Before we dive into the frameworks, I want to drop back to basics to give some context behind why the frameworks exist and what benefits they provide. For this reason, let's look at a naive model 1 implementation of the sample application. If you're already familiar with the whole model 1 vs. model 2 thing, you might want to skip reading this particular entry.
Overview
Home page
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="domain.*,
java.util.Iterator,
java.text.DateFormat" %>
<%
BlogService blogService = new BlogService();
Blog blog = blogService.getBlog();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><%= blog.getName() %></title>
<link rel="stylesheet" href="screen.css" type="text/css" />
</head>
<body>
<div id="container">
<h1><%= blog.getName() %></h1>
<h2><%= blog.getDescription() %></h2>
<%
Iterator it = blog.getBlogEntries().iterator();
while (it.hasNext()) {
BlogEntry blogEntry = (BlogEntry)it.next();
%>
<div class="blogEntry">
<h3><%= blogEntry.getTitle() %></h3>
<%
if (blogEntry.getExcerpt() != null) {
%>
<%= blogEntry.getExcerpt() %>
<p>
<a href="viewBlogEntry.jsp?id=<%= blogEntry.getId()%>">Read more</a>
</p>
<%
} else {
%>
<%= blogEntry.getBody() %>
<%
}
%>
<p>
<%
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG, blog.getLocale());
dateFormat.setTimeZone(blog.getTimeZone());
%>
Posted on <%= dateFormat.format(blogEntry.getDate()) %>
</p>
</div>
<%
}
%>
</div>
</body>
</html>
I'm not going to explain each and every line, but the basic structure breaks down as follows.
For the "read more" link, this is simply implemented as a call to the blog entry detail JSP page, passing the blog entry ID as a parameter. Notice how this is conditionally included, depending on whether the excerpt is null - it's a good example of the simple presentation logic that you'll find in many web applications. The date formatting code is another example.
Blog entry detail page
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="domain.*,
java.text.DateFormat" %>
<%
BlogService blogService = new BlogService();
Blog blog = blogService.getBlog();
BlogEntry blogEntry = blog.getBlogEntry(request.getParameter("id"));
if (blogEntry == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><%= blogEntry.getTitle() %> : <%= blog.getName() %></title>
<link rel="stylesheet" href="screen.css" type="text/css" />
</head>
<body>
<div id="container">
<h1><%= blog.getName() %></h1>
<h2><%= blog.getDescription() %></h2>
<div class="blogEntry">
<h3><%= blogEntry.getTitle() %></h3>
<%= blogEntry.getBody() %>
<p>
<%
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG, blog.getLocale());
dateFormat.setTimeZone(blog.getTimeZone());
%>
Posted on <%= dateFormat.format(blogEntry.getDate()) %>
</p>
</div>
</div>
</body>
</html>
The scriptlet at the top of the page is probably the most interesting because that piece of code is responsible for locating the requested blog entry or sending the user off to an error page, which is simply configured through the <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page>
Summary
»
Related Topics >>
J2EE Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|