JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Standard Tag Libraries (JSTL)

Part:   1  2 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

Now let's see the Servlet class generated by Tomcat server based on my JSP page with the "c:out" tag. The Servlet class is located at \local\jakarta-tomcat-4.1.18\work\standalone\localhost\_\hello_jstl_jsp.java:

package org.apache.jsp;

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

public class hello_jstl_jsp extends HttpJspBase {


  private static java.util.Vector _jspx_includes;

  private org.apache.jasper.runtime.TagHandlerPool 
     _jspx_tagPool_c_out_value;

  public hello_jstl_jsp() {
    _jspx_tagPool_c_out_value = 
       new org.apache.jasper.runtime.TagHandlerPool();
  }

  public java.util.List getIncludes() {
    return _jspx_includes;
  }

  public void _jspDestroy() {
    _jspx_tagPool_c_out_value.release();
  }

  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("\r\n");
      out.write("<!--%@ taglib uri=\"http://java.sun.com/jstl/core\""
         +" prefix=\"c\" %-->\r\n");
      out.write("<html>");
      out.write("<body>\r\n");
      if (_jspx_meth_c_out_0(pageContext))
        return;
      out.write("\r\n");
      out.write("</body>");
      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);
    }
  }

  private boolean _jspx_meth_c_out_0(javax.servlet.jsp.PageContext 
     pageContext)
          throws Throwable {
    JspWriter out = pageContext.getOut();
    /* ----  c:out ---- */
    org.apache.taglibs.standard.tag.el.core.OutTag _jspx_th_c_out_0 
       = (org.apache.taglibs.standard.tag.el.core.OutTag) 
          _jspx_tagPool_c_out_value.get(
          org.apache.taglibs.standard.tag.el.core.OutTag.class);
    _jspx_th_c_out_0.setPageContext(pageContext);
    _jspx_th_c_out_0.setParent(null);
    _jspx_th_c_out_0.setValue("Hello world!");
    int _jspx_eval_c_out_0 = _jspx_th_c_out_0.doStartTag();
    if (_jspx_th_c_out_0.doEndTag() == 
       javax.servlet.jsp.tagext.Tag.SKIP_PAGE)
      return true;
    _jspx_tagPool_c_out_value.reuse(_jspx_th_c_out_0);
    return false;
  }
}

As you can see, the "c:out" tag is replaced by method call. The method then interacts with the class org.apache.taglibs.standard.tag.el.core.OutTag, where the "out" tag is implemented. The implementation is probably very simple, may be just an out.write() statement.

JSTL in XML Style JSP Pages

Since I like to write JSP in XML style, I have to find out how to use JSTL in XML style JSP pages. It took me some time to figure this. Here is an example code, hello_jstl_xml.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:c="http://java.sun.com/jstl/core" version="1.2"> 
<!-- hello_jstl_xml.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
<br/>
<c:out value="Hello world! - from c:out"/>
</body></html>
</jsp:root>

The trick is to convert the tablib to a name space attribute in the jsp:root element.

JSTL Requirements

The requirements to use JSTL are:

  • A JSP server - Tomcat from apache.org.
  • A JSTL implementation - Tablib from apache.org, jstl.jar and standard.jar.
  • A JSTL document - JSTL specification from jcp.org.
  • JSP pages with JSTL tags - You write them.

Part:   1  2 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Standard Tag Libraries (JSTL)