"Hello world!" with JSTL

This section provides a tutorial example to test the installation of JSTL 1.2 JAR file developed by the Oracle GlassFish project. The example print out a hello message with the 'c:out' tag.

To understand how JSTL works, let's try a very simple example, using JSTL to say hello in the browser window. Here is my JSP source code, hello_jstl.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html><body>
<c:out value="Hello world! - From JSTL"/>
</body></html>

Save it to \local\apache-tomcat-7.0.32\webapps\ROOT, and run IE with URL: http://localhost:8080/hello_jstl.jsp. I got the prefect message "Hello world! - From JSTL" in the IE window!

Now let's see the Servlet class converted by Tomcat server from on my JSP page with the "c:out" tag. The Servlet class is located in the \local\apache-tomcat-7.0.32\work\Catalina\localhost\_\org\apache\jsp directory. Open the converted class file, hello_005fjstl_jsp.java:

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.32
 */
package org.apache.jsp;

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

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

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> 
     _jspx_dependants;

  private org.apache.jasper.runtime.TagHandlerPool _005fjspx_005fta...

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> 
    getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody 
       = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(
       getServletConfig());
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(
       getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager 
       = org.apache.jasper.runtime.InstanceManagerFactory
       .getInstanceManager(getServletConfig());
  }

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

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

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      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("\r\n");
      out.write("<html><body>\r\n");
      if (_jspx_meth_c_005fout_005f0(_jspx_page_context))
        return;
      out.write("\r\n");
      out.write("</body></html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) 
           _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }

  private boolean _jspx_meth_c_005fout_005f0(
          javax.servlet.jsp.PageContext _jspx_page_context)
          throws java.lang.Throwable {
    javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
    javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
    //  c:out
    org.apache.taglibs.standard.tag.el.core.OutTag 
       _jspx_th_c_005fout_005f0 
       = (org.apache.taglibs.standard.tag.el.core.OutTag)
       _005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody
          .get(org.apache.taglibs.standard.tag.el.core.OutTag.class);
    _jspx_th_c_005fout_005f0.setPageContext(_jspx_page_context);
    _jspx_th_c_005fout_005f0.setParent(null);
    // /hello_jstl.jsp(3,0) name = value type 
       = java.lang.String reqTime = false required 
       = true fragment = false deferredValue 
       = false expectedTypeName = null deferredMethod 
       = false methodSignature = null
    _jspx_th_c_005fout_005f0.setValue("Hello world! - From JSTL");
    int _jspx_eval_c_005fout_005f0 
       = _jspx_th_c_005fout_005f0.doStartTag();
    if (_jspx_th_c_005fout_005f0.doEndTag() 
       == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
      _005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody
         .reuse(_jspx_th_c_005fout_005f0);
      return true;
    }
    _005fjspx_005ftagPool_005fc_005fout_0026_005fvalue_005fnobody
       .reuse(_jspx_th_c_005fout_005f0);
    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.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

Overview of JSTL (JSP Standard Tag Libraries)

 What is JSTL?

 Installing GlassFish JSTL 1.2 on Tomcat 7

"Hello world!" with JSTL

 JSTL in XML Style JSP Pages

 General Syntax of JSTL Tags

 NoClassDefFoundError - LoopTag/ForEachTag

 Installing JSTL 1.2 API JAR File

 JSTL Example - Loop Tag on Map Objects

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version