Java Reference
In-Depth Information
19.1
W HAT Y OU W ILL L EARN
• Theory of operation: how JSP can be thought of as servlets “inside out.”
• Three simple JSP directives: scriptlet , declaration , expression .
• Servlet variables made available: request , response , out , session .
• Server-side includes.
• A tiny bit about tags.
jsp:useBean .
• A look at our BudgetPro using JSP.
• The correct spelling of JavaServer Pages.
19.2
S ERVLETS T URNED I NSIDE O UT : 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 :
Search WWH ::




Custom Search