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\:

<!-- Hello_Scripting.jsp
   Copyright (c) 2002 All Rights Reserved.
<%= greeting() %>
<% out.println("Hello world!"); %>
<%= "-- From JSP" %>
   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,, 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 =

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

  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 =
    _jsp_instancemanager =

  public void _jspDestroy() {

  public void _jspService(
        final javax.servlet.http.HttpServletRequest request,
        final javax.servlet.http.HttpServletResponse response)
        throws, 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 {
      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("<!-- Hello_Scripting.jsp\r\n");
   "   Copyright (c) 2002 All Rights Reserved.\r\n");
      out.print( greeting() );
 out.println("Hello world!");

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


    } 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 ( e) {}
        if (_jspx_page_context != null)
        else throw new ServletException(t);
    } finally {

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\webapps\ROOT\WEB-INF\classes>javac \

   3,716 Hello_005fScripting_jsp.class

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=""




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


 Full Version in PDF/EPUB