Modifying the Servlet Converted from a JSP Page

This section provides a tutorial example on how to capture and modify the Servlet class converted from a JSP by the Tomcat server.

Since I learned that a JSP page is served on Apache Tomcat by executing the JSP Servlet Java class converted from the JSP page, I wanted to see if I capture the Servlet class converted from a JSP page by Tomcat and deploy it as a true Servlet.

1. Reuse the JSP page I created previously, Hello_Scripting.jsp, and save it to C:\local\tomcat\webapps\ROOT\:

<html><body>
<!-- Hello_Scripting.jsp
   Copyright (c) 2002 HerongYang.com. All Rights Reserved.
-->
<pre>
<%= greeting() %>
<% out.println("Hello world!"); %>
<%= "-- From JSP" %>
</pre>
</body></html>
<%!
   private String greeting() {
      return "Scripting Elements:";
   }
%>

2. To force Tomcat to convert the JSP page, run a Web browser and enter this URL: http://localhost:8080/Hello_Scripting.jsp. I see this output on the browser screen:

Scripting Elements:
Hello world!

-- From JSP

3. Open the Servlet class, Hello_005fScripting_jsp.java, converted from Hello_Scripting.jsp by Tomcat at \local\tomcat\work\Catalina\localhost\_\org\apache\jsp\ and modify just one line:

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

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

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

   private String greeting() {
      return "Scripting Elements:";
   }

  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 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() {
    _el_expressionfactory =
          _jspxFactory.getJspApplicationContext(
          getServletConfig().getServletContext()
          ).getExpressionFactory();
    _jsp_instancemanager =
org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(
          getServletConfig());
  }

  public void _jspDestroy() {
  }

  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("<html><body>\r\n");
      out.write("<!-- Hello_Scripting.jsp\r\n");
      out.write(
   "   Copyright (c) 2002 HerongYang.com. All Rights Reserved.\r\n");
      out.write("-->\r\n");
      out.write("<pre>\r\n");
      out.print( greeting() );
      out.write('\r');
      out.write('\n');
 out.println("Hello world!");
      out.write('\r');
      out.write('\n');

      // out.print( "-- From JSP" );
      out.print( "-- From JSP converted Servlet" );

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

    } 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);
    }
  }
}

4. Save the modified Servlet class to the Servlet class folder: \local\tomcat\webapps\ROOT\WEB-INF\classes\org\apache\jsp

5. Compile the modifed Servlet class in a command window:

herong> set classpath=\local\tomcat\lib\servlet-api.jar; \
   \local\tomcat\lib\jsp-api.jar;                    \
   \local\tomcat\lib\jasper.jar;                     \
   \local\tomcat\lib\el-api.jar;                     \
   \local\tomcat\lib\tomcat-api.jar

\local\tomcat\webapps\ROOT\WEB-INF\classes>javac \
   Hello_005fScripting_jsp.java

\local\tomcat\webapps\ROOT\WEB-INF\classes>dir
   3,716 Hello_005fScripting_jsp.class
   3,429 Hello_005fScripting_jsp.java

5. Map the modified Servlet to a URL path name on the Tomcat server by editing the "\local\tomcat\webapps\ROOT\web.xml" file:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

...

  <servlet>
    <servlet-name>Hello_Scripting</servlet-name>
<servlet-class>org.apache.jsp.Hello_005fScripting_jsp</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello_Scripting</servlet-name>
    <url-pattern>/Hello_Scripting.servlet</url-pattern>
  </servlet-mapping>

</web-app>

6. Access the Servlet class through the mapped URL "http://localhost:8080/Hello_Scripting.servlet" with any browser. The output of my first Servlet class shows up:

Scripting Elements:
Hello world!

-- From JSP converted Servlet

Congratulations! I have successfully modified a Servlet class converted from a JSP page by the Tomcat server!

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

Java Servlet Introduction

 What Is Servlet

 Creating and Deploying a Servlet on Tomcat

 Relationship between Servlet and JSP

Modifying the Servlet Converted from a JSP Page

 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)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB