- What You Will Learn
- Servlets Turned Inside Out: JSP
- How to Write a JSP Application
- Using JSP with BudgetPro
- Review
- What You Still Don't Know
- Resources
- Exercises
19.2 Servlets Turned Inside Out: JSP
Take a look at the AccountView.java class in the BudgetPro servlet example. It consists almost entirely of
sb.append("</a></td>");
method calls which build up a string of HTML. Instead, this could have been calls to do the output right then and there:
out.println("</a></td>");
Either way, if we want to modify the HTML, we have to modify the Java code. While that's not difficult, it can be error-prone. It would be nice to not have the Java syntax in the way when we want to modify the HTML. (That's especially true when you want to put quotation marks in your HTML:
out.println("<input name=\"name\" size=\"20\">");
It's not that it can't be done; the \" just gets hard to read and hard to get right the first time.)
One way to externalize all the HTML is to put it into a file. Then our Java application could read the file at runtime and send its contents to the browser. Not bad, but what about the dynamic parts? Remember how we generated the table from the for loop in AccountView.java:
for (Iterator actit = acct.getAllSubs(); actit.hasNext(); ) { Account suba = (Account) actit.next(); sb.append("<tr>"); sb.append("<td><a href=\"BudgetPro?name="+suba.getName()); sb.append("&func=cd\">"); sb.append(suba.getName()); sb.append("</a></td>"); sb.append("<td>albing</td>"); sb.append("<td>"); sb.append(suba.getTotal().toString()); sb.append("</td>"); sb.append("</tr>\n"); } // next acct
That would be hard to do with file-based HTML.
Another approach, the one used by JavaServer Pages, would be to use the HTML file as input to a converter program—one which would take each line of HTML, for example
<input name="name" size="20">
and produce a line of Java code:
out.println("<input name=\"name\" size=\"20\">");
Notice how the converter would be the one to handle the escape sequence for the quotation marks; we get to write straight HTML—it has to deal with the backslashes.
This is the basic idea behind JavaServer Pages. JSP files are nothing more than HTML (with some additions that we'll discuss shortly) which are compiled into Java programs—servlets, to be exact—that are then run to produce the Web page. The conversion happens no later than the first time the Web server tries to serve up that JSP. If it hasn't yet been converted, it will convert it into Java code and start the servlet. Thereafter, other requests to that page go directly to the servlet. If you modify the JSP file, then the Web server recognizes that the file has been modified and reconverts it.
But why go to all this trouble? It's not for the static HTML that we need JSP, but rather for the dynamic bits. Remember that for loop, above, used to make the HTML table of subaccounts? Let's look at part of a JSP that does the same thing:
<table border=1 width=50%> <tr> <th>Account</th> <th>Owner</th> <th>Value</th> </tr> <% // for each subaccount: for (Iterator actit = acct.getAllSubs(); actit.hasNext(); ) { Account suba = (Account) actit.next(); %> <tr> <td><a href="BPControl?name=<%= suba.getName() %>&func=cd"> <%= suba.getName() %> </a></td> <td>albing</td> <td> <%= suba.getTotal().toString() %> </td> </tr> <% } // next acct %> </table>
Notice how it starts off as simply the HTML building the table opening. Then we encounter some Java source code, enclosed in delimiters (<% ... %>), then back to plain HTML. There's even a line which intermixes HTML and Java:
<td><a href="BPControl?name=<%= suba.getName() %>&func=cd">
To understand what's going on here, let's take a look at four pieces of syntax that are the keys to JSP.