Chapter 28
JavaServer Pages
JavaServer Pages (JSP) was created as a next step in servlets’ evolution. JSP 2.2 is part of the Java EE 6 specification, and with it you can do everything that you can do with servlets, but more easily. Let’s say you’ve created and deployed a servlet, which displays “Hello World.” The servlet gets a hold of the output stream of the response object and executes the following line of code:
out.println("<html><body>Hello World </body></html>");
Let’s say you run a small software company that employs Alex, an expensive Java developer, and Matilda, a junior Web designer who doesn’t know Java but does know HTML. What if you need to change the layout of this HTML page, such as by adding several empty lines on top? It’s not a big problem — Alex can modify the preceding line of code, recompile it, and redeploy the servlet. But for making small changes in the HTML-based UI it’s more efficient to use Matilda. This is where JSP becomes very handy. Ask Matilda to create the following text file, HelloWorld.jsp:
<html> <body> Hello World </body> </html>
Place this file into the document root directory (see Lesson 27) in your servlet container running at, say, MyBooks.com. Now the users can access this JSP by entering the following URL in their web browsers:
http://www.MyBooks.com/HelloWorld.jsp
Upon the first request to this page, the JSP container (vendors of servlet containers support JSP, too) will automatically generate, compile, and deploy ...