Replacing Tomcat's ROOT index.jsp page
Apache
Tomcat
comes with a simple ROOT webapp that is nothing
more than a precompiled index JSP page.
The text of this JSP page warns users not to bother trying to edit this index.jsp page in the
$CATALINA_HOME/webapps/ROOT
directory.
If you try,
then reload the
http://localhost:8080/
page,
your changes won’t be reflected because the index page was precompiled into a JAR file.
Nowhere in the Tomcat HowTo page did I see a quick pointer to changing the contents of the index.jsp page so Tomcat beginners can experiment quickly with Tomcat and JSPs. The closest I could find was a How To that tells users they can write an index.html page and have that override the index.jsp page.
So here’s my quick pointer instructions. For those of you familiar with Tomcat and servlets, you will see nothing new here. But for Tomcat and J2EE beginners, I hope I’ll save you a few minutes digging through the ROOT webapp’s configuration files if you want to start playing with JSPs soon after installing Tomcat 5.5.
How do I edit 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 (changes in red):
<!-- JSPC servlet mappings start --> <!-- Disabling the index_jsp servlet <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.
I’ve added the above instructions to the Tomcat HowTo wiki in the hope it helps Tomcat newcomers find their way around the server.
Updated 2007-7-11:
- Fixed typo in XML comment tag. Thank you Peter Fischer for pointing this out.
- Note: Tomcat 6 simplified its ROOT webapp index page, so these instructions don’t apply.