Editing the default JSP home page loaded by Tomcat

The contents of the default Tomcat home page comes from the ROOT webapp servlet called org.apache.jsp.index_jsp. The page that you see in $CATALINA_HOME/webapps/ROOT/index.jsp
has been precompiled into a class file (org.apache.jsp.index_jsp.class) stored in a JAR file (catalina-root.jar) in the ROOT webapp's WEB-INF/lib directory. Because of this servlet, Tomcat will not look at the contents of the ROOT web application's
index.jsp file if you change it.

The easiest way to change the contents of the index.jsp page is to remove this
index_jsp servlet from the ROOT webapp. Once you remove the index_jsp servlet and restart Tomcat, Tomcat will see the index.jsp file in the ROOT directory and compile it on the fly into a class file. You now will be able to edit the ROOT/index.jsp file and have those changes take effect immediately by reloading the http://localhost:8080/ page.

To remove the index_jsp servlet, edit the ROOT web application's configuration file,
$CATALINA_HOME/webapps/ROOT/WEB-INF/web.xml. Comment out the definition of the servlet and the servlet mapping, so that section of the file will look like this (note the commented out section):

<!-- JSPC servlet mappings start -->

  <!-- Commenting out so I can change the index.jsp page
    <servlet>
        <servlet-name>org.apache.jsp.index_jsp</servlet-name>
        <servlet-class>org.apache.jsp.index_jsp</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>org.apache.jsp.index_jsp</servlet-name>
        <url-pattern>/index.jsp</url-pattern>
    </servlet-mapping>
  -->
<!-- JSPC servlet mappings end -->

Once you disable the index_jsp servlet and restart Tomcat, how does Tomcat know to compile the
index.jsp page in the ROOT web app's directory? Easy. First, when you request the default page of a web application, Tomcat (like every servlet container) will look for a welcome file. The default welcome files are defined at the bottom of $CATALINA_HOME/conf/web.xml. This web.xml file acts as a global web.xml file used for all web applications installed in Tomcat. The default welcome file list includes index.jsp, which means Tomcat will try to load that file (if found) in order to display it. Second, the $CATALINA_HOME/conf/web.xml configuration file also defines a servlet called simply jsp. This section of the web.xml file:

    <!-- The mapping for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>

maps all .jsp and .jspx pages to the jsp servlet. The jsp servlet performs the work of compiling the source JSP file into a servlet and then executing the servlet. The JSP servlet, by default, will check the JSP source page every time it is requested to see if it was modified since the last time it was compiled. If the page changed within 4 seconds of the last time it was compiled, the servlet will recompile the source JSP page before running it. The behavior of the jsp servlet is quite configurable. You can see all its options defined in the
$CATALINA_HOME/conf/web.xml configuration file.

  • No labels