The following is an example of a JavaServer Page.
<% java.util.Date dateToday = new java.util.Date(); %> <html> <body> <p>Todays date is <%= dateToday %> </p> </body> </html>
Simply, a JSP contains a mix of HTML and Java code where the Java code handles all the page logic and the HTML manages the display. We will look later at the syntax of a JSP but for now let us focus on what happens when a JSP page is requested.
When the JSP container receives the first request for a JavaServer Page it is required to convert the JSP into a Java Servlet. This Servlet is then executed and the output of this execution is sent as the response to the initial request.
In the example above the text between <% and %>
is our Java code which, when executed, assigns the variable
dateToday the current system date. The value of this variable is written
to the HTML in the line <P>Todays date is <%= dateToday %>
</P> . The HTML is then sent as the response to the client.
Click the following icon to see the result of this JSP. When the page is displayed view the source HTML of the file. To do this in Internet Explorer right click over the window and select "View Source".
Spot the difference ? The HTML is exactly the same but all the Java code has been processed and removed from the output stream.
Now it was mentioned earlier that the JSP is converted to a Java Servlet. Java Servlets were Suns first server based processing using the Java language. However they were never a great success because all the HTML had to be contained within valid Java syntax which meant that changing a page became a real headache. The following is the source code for the Servlet that the above example is converted to.
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import allaire.jrun.jsp.JRunJSPStaticHelpers;
public class jrun__JSPModule__ShowDate2ejsp17
extends allaire.jrun.jsp.HttpJSPServlet
implements allaire.jrun.jsp.JRunJspPage
{
private ServletConfig config;
private ServletContext application;
private Object page = this;
private JspFactory __jspFactory =
JspFactory.getDefaultFactory();
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, java.io.IOException
{
if(config == null)
{
config = getServletConfig();
application = config.getServletContext();
}
response.setContentType("text/html; charset=ISO-8859-1");
PageContext pageContext =
__jspFactory.getPageContext(this,request,response,
null,true,8192,true);
JspWriter out = pageContext.getOut();
HttpSession session = pageContext.getSession();
try
{
Date dateToday = new Date();
out.print("\r\n\r\n<html>\r\n<body>\r\n<p>
Today's date is ");
out.print(dateToday);
out.print(".<p>\r\n</body>\r\n</html>");
}
catch(Throwable t)
{
if(t instanceof ServletException)
throw (ServletException) t;
if(t instanceof java.io.IOException)
throw (java.io.IOException) t;
if(t instanceof RuntimeException)
throw (RuntimeException) t;
throw JRunJSPStaticHelpers.handleException
(t, pageContext);
}
finally
{
__jspFactory.releasePageContext(pageContext);
}
}
}
So comparing the JSP above to the equivalent Servlet it should be obvious why JSP technology is better for web application development. Rememeber that, unlike standard applications, web applications are more susceptible to change particularly their look and feel. In JavaServer Pages having the HTML clearly separate from the Java code makes maintaining and updating web applications far easier to achieve.
Once the JSP has been rewritten as a Servlet it is still in source code. The JSP Container then compiles the Servlet to an executable Java class file. The JSP Container controls this process and makes use of the Java Development Kit, JDK, to do the compilation. See side bar for more details on JDK. Once this has been done the JSP Container then executes the class file using the JVM.
Compare the Servlet source code with the JavaServer Page code above. Can you see where the Java code and HTML are included in the Servlet code?
The next section discusses the software required for JavaServer Pages and includes a general description of installing an Application Server.