How Does JSP Work

I l @ ve RuBoard

To begin, let's look at the world's simplest JSP page, our version of the infamous "Hello World" program (see Listing 1.1).

Listing 1.1 HelloWorld.jsp
 <HTML>  <HEAD><TITLE>Hello World!</TITLE></HEAD>  <BODY>Hello World!</BODY> </HTML> 

Okay, I can hear the screams of protest already. "That isn't JSP ”that's plain old HTML. What are you trying to pull? I want my money back!"

It might look like innocent HTML, and if you put it into a .html file on a Web server, it would be delivered as straight HTML. But look what happens to this innocent little snippet in Listing 1.2 if you put it into a .jsp file instead.

Listing 1.2 HelloWorld as Java
 package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import org.apache.jasper.runtime.*; public class _0002fjsp_0002fhello_0005fworld_jsp extends HttpJspBase {     static {     }     public _0002fjsp_0002fhello_0005fworld_jsp( ) {     }     private static boolean _jspx_inited = false;     public final void _jspx_init() throws org.apache.jasper.runtime.JspException {     }     public void _jspService(HttpServletRequest request, HttpServletResponse  response)         throws java.io.IOException, ServletException {         JspFactory _jspxFactory = null;         PageContext pageContext = null;         HttpSession session = null;         ServletContext application = null;         ServletConfig config = null;         JspWriter out = null;         Object page = this;         String  _value = null;         try {             if (_jspx_inited == false) {                 synchronized (this) {                     if (_jspx_inited == false) {                         _jspx_init();                         _jspx_inited = true;                     }                 }             }             _jspxFactory = JspFactory.getDefaultFactory();             response.setContentType("text/html;charset=ISO-8859-1");             pageContext = _jspxFactory.getPageContext(this, request, response,                       "", true, 8192, true);             application = pageContext.getServletContext();             config = pageContext.getServletConfig();             session = pageContext.getSession();             out = pageContext.getOut();             // HTML // begin [file="/jsp/hello_world.jsp";from=(0,0);to=(4,0)]                 out.write("<HTML>\r\n <HEAD><TITLE>Hello World!</TITLE></HEAD>\r\n graphics/ccc.gif <BODY>Hello World!</BODY>\r\n</HTML>\r\n");             // end         }  catch (Throwable t) {             if (out != null && out.getBufferSize() != 0)                 out.clearBuffer();             if (pageContext != null) pageContext.handlePageException(t);         }  finally {             if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);         }     } } 

Our simple HTML has been turned into Java code! However, if you look carefully , you'll see this line:

 out.write("<HTML>\r\n <HEAD><TITLE>Hello World!</TITLE></HEAD>\r\n <BODY>Hello World!</ graphics/ccc.gif BODY>\r\n</HTML>\r\n"); 

The HTML is still living happily, embedded as a write statement inside the Java.

This example is important because it demonstrates the most important thing to remember about JSP. Even though you might think at the time that you're writing HTML with Java code embedded, you're really always writing Java code with HTML embedded.

Of course, if you used JSP only to output static HTML content, it would be a huge waste of resources and performance. JSP is great because programmatic functionality can be embedded into HTML. For example, let's look at a very simple JSP page in Listing 1.3.

Listing 1.3 5loop.jsp
 <HTML>  <HEAD><TITLE>Hello World!</TITLE></HEAD>  <BODY>  <%    int i = 0;    while (i++ < 5) {  %>   Hello World! Loop #   <% out.print(i); %>   <br>  <% }  %> </BODY> </HTML> 

We've suddenly corrupted the pure HTML file by placing Java code in the middle. The Java is delimited by the <% and %> characters . Now turn your thinking around 180 °. We don't have an HTML file with a little Java embedded in it; we have a Java source with some HTML in it. Remember that all HTML in the source is going to turn into out.print statements in the intermediate .java file. So, when Java is placed in a JSP file, it is really escaping into the "real" world and out of the artificial HTML world. Just this once, we're going to look at a portion of the .java file in Listing 1.4 (leaving out most of the boilerplate from the "Hello World" example).

Listing 1.4 A Java Fragment of JSP Code
 // HTML // begin [file="/jsp/5loop.jsp";from=(0,0);to=(3,1)]                 out.write("<HTML>\r\n <HEAD><TITLE>Hello World!</TITLE></HEAD>\r\n <BODY>\r\n ");             // end             // begin [file="/jsp/5loop.jsp";from=(3,3);to=(6,1)]                    int i = 0;                    while (i++ < 5) {             // end             // HTML // begin [file="/jsp/5loop.jsp";from=(6,3);to=(8,2)]                 out.write(" \r\n  Hello World! Loop #\r\n  ");             // end             // begin [file="/jsp/5loop.jsp";from=(8,4);to=(8,19)]                  out.print(i);             // end             // HTML // begin [file="/jsp/5loop.jsp";from=(8,21);to=(10,1)]                 out.write("\r\n  <br>\r\n ");             // end             // begin [file="/jsp/5loop.jsp";from=(10,3);to=(10,6)]                  }             // end             // HTML // begin [file="/jsp/5loop.jsp";from=(10,8);to=(14,0)]                 out.write("\r\n\r\n</BODY>\r\n</HTML>\r\n"); 

As you can see, the Java code from the JSP file is included verbatim, while the HTML is put inside print statements. Although it might look strange to see HTML code wrapped inside a Java while loop in the .jsp file, it makes perfect sense when you see the resulting output file.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net