How Does a JSP Work?
Home ] Up ] What's  a Java Server Page? ] [ How Does a JSP Work? ] Do JSPs Replace Servlets? ] JSP Expressions vs. Scriplets ] Conditional JSPs ] JSP Declarations ] A JSP Hit Counter ]

 

 

The Servlet Container makes a JSP work by converting it into a servlet.

So, everything you learned about servlets is still relevant for JSPs.

For our very simple example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>A Simple JSP</title>
    </head>
    <body>

    <h1>Greetings!</h1>
    <p>This is a 'Hello!' from <i><%= request.getParameter("name") %></i>.</p>
    <p>How are you today?</p>
      
    </body>
</html>

This is the corresponding generated servlet code (as generated by the built-in Tomcat server container that comes with NetBeans 5.5):

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

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

  private static java.util.List _jspx_dependants;

  public Object 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();
      response.setContentType("text/html");
      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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n");
      out.write("   \"http://www.w3.org/TR/html4/loose.dtd\">\n");
      out.write("\n");
      out.write("<html>\n");
      out.write("    <head>\n");
      out.write("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
      out.write("        <title>A Simple JSP</title>\n");
      out.write("    </head>\n");
      out.write("    <body>\n");
      out.write("\n");
      out.write("    <h1>Greetings!</h1>\n");
      out.write("    <p>This is a 'Hello!' from <i>");
      out.print( request.getParameter("name") );
      out.write("</i>.</p>\n");
      out.write("    <p>How are you today?</p>\n");
      out.write("      \n");
      out.write("    </body>\n");
      out.write("</html>\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          out.clearBuffer();
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

Note how the straight HTML from the JSP is output directly using a series of out.write() calls, while the contents of the JSP tag (here, a Java expression) is extracted, and also placed in an out.write() call.

 
The page was last updated February 19, 2008