13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

424Chapter 19JSP: Servlets Turned Inside Out19.1WHAT YOU WILL LEARN• Theory of operati<strong>on</strong>: how JSP can be thought of as servlets “inside out.”• Three simple JSP directives: scriptlet, declarati<strong>on</strong>, expressi<strong>on</strong>.• Servlet variables made available: request, resp<strong>on</strong>se, out, sessi<strong>on</strong>.• Server-side includes.• A tiny bit about tags.• jsp:useBean.• A look at our BudgetPro using JSP.• The correct spelling of JavaServer Pages.19.2SERVLETS TURNED INSIDE OUT: JSPTake a look at the AccountView.java class in the BudgetPro servlet example.It c<strong>on</strong>sists almost entirely ofsb.append("");method calls which build up a string of HTML. Instead, this could have beencalls to do the output right then and there:out.println("");Either way, if we want to modify the HTML, we have to modify the Javacode. While that’s not difficult, it can be error-pr<strong>on</strong>e. It would be nice to nothave the Java syntax in the way when we want to modify the HTML. (That’sespecially true when you want to put quotati<strong>on</strong> marks in your HTML:out.println("");It’s not that it can’t be d<strong>on</strong>e; the \" just gets hard to read and hard to get rightthe first time.)One way to externalize all the HTML is to put it into a file. Then our Javaapplicati<strong>on</strong> could read the file at runtime and send its c<strong>on</strong>tents to the browser.Not bad, but what about the dynamic parts? Remember how we generated thetable from the for loop in AccountView.java:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!