The Servlets Generated by JSPs

 

Introduction.  Now we need to examine servlets just a bit so we can start to become familiar with them.  We will do this by working through two of the simplest JSP pages we have done so far.
  • hello_world.jsp
  • cubes.jsp

I find it a very intriguing experience to see what servlet code is generated even by simple JSP pages!

At present you need to make sure you have uploaded hello_world.jsp and cubes.jsp to your jasperations.net directory

/htdocs/yourLastName

and then execute them.  Now you need to go to

/catalina/temp/yourLastName

and download

  • hello_world_jsp.java

  • cubes_jsp.java

so you can view them.

These servlets can also be useful in debugging since it is often the case that the line numbers displayed in error reports correspond to line numbers in these servlets!

I will try to place the source JSP and generated servlets side-by-side to assist comparison.

 

Original hello_world.jsp Generated Servlet Code for hello_world_jsp.java
<html>
<head>
<title>Hello World JSP Page</title>
</head>

<body>
<%

out.println("<H1>Hello World</H1>");

%>

</body>
</html>
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class hello_world_jsp extends HttpJspBase
{

private static java.util.Vector _jspx_includes;

public java.util.List getIncludes( )
{

return _jspx_includes;

}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException
{

JspFactory _jspxFactory = null;
javax.servlet.jsp.PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;


try
{

_jspxFactory = JspFactory.getDefaultFactory( );
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true);
application = pageContext.getServletContext( );
config = pageContext.getServletConfig( );
session = pageContext.getSession( );
out = pageContext.getOut( );
_jspx_out = out;

out.write("<html>\r\n");
out.write("<head>\r\n\t");
out.write("<title>Hello World JSP Page");
out.write("</title>\r\n");
out.write("</head>\r\n\r\n");
out.write("<body>\r\n");


out.println("<H1>Hello World</H1>");


out.write("\r\n\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");

}
catch (Throwable t)
{

out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer( );
if (pageContext != null) pageContext.handlePageException(t);

}
finally
{

if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);

}

}

}

 

I rest my case for not taking the usual academic approach and digging into servlets in order to do JSPs!

Though, I must acknowledge, this particular servlet engine is unusually cryptic.

The following is the code for the cubes.jsp and associated generated servlet.

 

Original cubes.jsp Generated Servlet Code for cubes_jsp.java
<html>
<head>
<title>Calculating Cubes</title>
</head>

<body bgcolor = "000044" text="cccccc">
<%
out.println("<h2><B>Cubes</B></h2><BR>");
int iLoop;
int iCube;
for (iLoop = 1; iLoop <= 10; iLoop++)
{

iCube = iLoop*iLoop*iLoop;
out.println("<font size=4>The cube of " + iLoop + " is <b>" + iCube + "</b></font><br>");

}
%>
</body>
</html>

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class cubes_jsp extends HttpJspBase
{

private static java.util.Vector _jspx_includes;

public java.util.List getIncludes( )
{

return _jspx_includes;

}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException
{

JspFactory _jspxFactory = null;
javax.servlet.jsp.PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;


try {

_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<title>Calculating Cubes");
out.write("</title>\r\n");
out.write("</head>\r\n\r\n");
out.write("<body bgcolor = \"000044\" text=\"cccccc\">\r\n");

out.println("<h2><B>Cubes</B></h2><BR>");
int iLoop;
int iCube;
for (iLoop = 1; iLoop <= 10; iLoop++)
{

iCube = iLoop*iLoop*iLoop;
out.println("<font size=4>The cube of " + iLoop + " is <b>" + iCube + "</b></font><br>");

}

out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");

}
catch (Throwable t)
{

out = _jspx_out;
if (out != null && out.getBufferSize( ) != 0)
out.clearBuffer( );
if (pageContext != null) pageContext.handlePageException(t);

}
finally
{

if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);

}

}

}

 

We will endeavor to discuss these in class!