JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies

Born from Servlets

Before going into what is needed with regard to application servers, it is helpful to understand how Java Server Pages run within that application server.

I hesitate to completely confuse people, but Java Server Pages actually turn into Servlets within the container when they are requested by the end-user. Java Server Pages are so powerful for this very reason. They give you the ability to manipulate the page’s presentation of dynamic data while leveraging HTML without having to do complex server side coding, and yet you reap the power of the server side coding.

More on this process is explained in the following section, Application Server Architecture, but it is always good to know this background so you do not look at someone like they are nuts if they suggest that Java Server Pages are nothing more than fancy Servlets.

Below I have included two “Hello World” code samples, one using a Servlet to create the content and another using a JSP to create the content. Both methods produce the same output. This sample with code comments is designed to allow Servlets and JSPs to relate to each other, as JSPs are an abstraction layer on top of Servlets. Do not worry about the details of the Servlet code, as we will dig deeper into the code later in the book.

HelloWorld Servlet

package org.apache.jsp;import javax.Servlet.*;
import javax.Servlet.http.*;
import javax.Servlet.jsp.*;

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {

private static java.util.Vector _jspx_dependants;

public java.util.List getDependants() {
return _jspx_dependants;

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;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;

try {
_jspxFactory = JspFactory.getDefaultFactory();
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write(” <head>\n”);
out.write(” <title>JSP and Beyond Hello World</title>\n”);
out.write(” </head>\n”);
out.write(” <body>\n”);
out.write(” Hello World \n”);
out.write(” </body>\n”);
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);

HelloWorld JSP Page

<%@page contentType=”text/html”%>
<title>JSP and Beyond - Hello World</title>
<!– This is our Hello World code –>
<%= out.println(”Hello World”) %>

This is the HTML that is produced from the above Servlet and the JSP code!

<title>JSP and Beyond Hello World</title>
Hello World

Which one do you think took more effort to write? Which one makes it easier to manage the layout of the page? Even though we do not have to do complex coding in the JSP the Java Application Server will handle all of the translation to Servlet code for us as JSPs are transformed into Servlets when they are run.

On the highlighted lines in the code for the Servlet above it is interesting to notice that the out.write lines produce the HTML lines that we see in the HelloWorld.JSP code. This is much the equivalent of doing a Response.Write using Microsoft ASP or ASP.NET.

Speaking of Java Application Servers let’s look at how the HelloWorld.JSP page above was processed to produce the HelloWorld Servlet that is ultimately executed to render the content above in the Application Server section below.