Applications built using a client-server approach have been around for some time; they consist of one or more application programs running on client machines, and connecting to a server-based application to work. (A good example would be a PowerBuilder or Oracle Forms based system.) CGIs and pre-Servlet applications were generally based on this simple 2-tier model, and with the introduction of Servlets, 2-tier applications could also be created in Java.
This model allows JSPs or Servlets direct access to some resource like a database or legacy application to service a client's request: the early JSP specifications termed this a "Model 1" programming approach. The JSP page is where the incoming request is intercepted processed, and the response sent back to the client; JSPs only differed from Servlets in this scenario by providing cleaner code, and separating code from the content by placing data access in beans.
This is illustrated in the figure below.
The advantage of such an approach is that it is simple to program, and allows the page author to generate dynamic content easily, based upon the request and the state of the resources.
However this architecture does not scale up well for a large number of simultaneous clients since there would be a significant amount of request processing to be performed, and each request must establish or share a potentially scarce/expensive connection to the resource in question. (A good example would be JDBC connections in servlets or JSPs and the need for connection pools.)
Indiscriminate usage of this architecture usually leads to a significant amount of Java code embedded within the JSP page. This may not seem to be much of a problem for Java developers but it is certainly an issue if the JSP pages are maintained by designers: the code tends to get in the designer's way, and you run the risk of your code becoming corrupted when others are tweaking the look and feel.
Of course, it is not necessary to use JSPs, to separate code and presentation in servlets: the use of templates is popular. There are a lot of toolkits that facilitate this, such as ATP (http://www.winwinsoft.com/atp/), MindTemplate (http://www.mindbright.se/english/technology/products/mindtemplate), or the more popular WebMacro (http://www.webmacro.org/), FreeMarker (http://freemarker.sourceforge.net), and PreparedHtml (http://www.its.washington.edu/~garth/prep.html)
This basic architecture involves direct request invocations to a server page with embedded Java code, and markup tags which dynamically generate output for substitution within the HTML.
This approach has many benefits. It is very easy to get started and is a low-overhead approach from a development standpoint. All of the Java code may be embedded within the HTML, so changes are confined to a limited area, reducing complexity. The figure below shows the architecture visually.
The big trade-off here is in the level of sophistication. As the scale of the system grows, some limitations of this approach surface, such as including too much business logic in the page instead of factoring forward to a mediating Servlet, or factoring back to a worker bean. As we discussed earlier, utilizing a Servlet and helper beans allow us to separate developer roles more cleanly, and improves the potential for code reuse. Let's start with a fairly simple example JSP page that follows this model, handling requests directly and including all the Java code within the JSP source page.
We begin with a basic description of the example application. All the examples in this chapter use the JSWDK version 1.0.1, using the default web application directory structure for simplicity. Thus, the JSP and HTML source are under the <jswdk-root>/examples/jsp/ subdirectory and the Servlet (see later) is under <jswdk-root>/examples/WEB-INF/servlets.
The user interface is shown below:
The HTML code for this page, BabyGame1.html, is stored in <jswdk-root>/examples/jsp/pageview/ and includes a form that prompts a user for selections. The user is asked to make some guesses about the statistics of a baby to be born in the near future. The HTML code is as follows:
<form method="post" action="BabyGame1.jsp" name="">
<table BORDER COLS=5 WIDTH="75%" >
<caption>Please enter your own name:
<input type="text" name="guesser"></caption>
<br><input type="radio" name="gender" value="Female" checked>Female
<p><input type="radio" name="gender" value="Male">Male
<td><font size=-1>Please choose a date:</font>
<p>Month: <select name="month">
<p>Day: <select name="day">
<td><font size=-1>Please choose a weight:</font>
<p>Pounds: <select name="pounds">
<p>Ounces: <select name="ounces">
<td><font size=-1>Please choose a length:</font>
<p>Inches: <select name="length">
</select><p> <p> <p> </td>
<p><input type="submit" name="submit" value="Make Guess">
<input type="reset" name="reset" value="Reset">