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.

19.3 How to Write a JSP <str<strong>on</strong>g>Applicati<strong>on</strong></str<strong>on</strong>g>427• • • • Let’s look at them <strong>on</strong>e at a time.19.3.1 ScriptletThe code that appears between the delimiters is called a scriptlet. Bythe way, we really hate the term “scriptlet.” It seems to imply (falsely) a completenessthat isn’t there. It is too parallel to the term “applet,” which is acomplete Java program that runs inside a browser. A scriptlet isn’t necessarilya complete anything. It’s a snippet of code that gets dropped inside the codeof the servlet generated from the JSP source.Recall that servlets may have a doPost() and a doGet() methods, whichwe collapsed in our example by having them both call the doBoth() method.Same sort of thing is happening here with the JSP, and the doBoth() ends updoing all the output of the HTML. Any snippets of Java code from within the delimiters get dropped right in place between those output calls,becoming just a part of a method.It can be useful to keep this in mind when writing JSP. It helps you answerthe questi<strong>on</strong>s of scope—who has access to what, where are variables gettingdeclared and how l<strong>on</strong>g will they be around? (Can you answer that last questi<strong>on</strong>?Since any variable declared inside the will be in the JSP equivalentof our doBoth() method, then that variable will <strong>on</strong>ly be around for the durati<strong>on</strong>of that <strong>on</strong>e call to the doBoth(), which is the result of <strong>on</strong>e GET (or POST)from the browser.)The source code snippets can be just pieces of Java, so l<strong>on</strong>g as it makes acomplete c<strong>on</strong>struct when all is c<strong>on</strong>verted. For example, we can write:

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

Saved successfully!

Ooh no, something went wrong!