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
If you try,
then reload the
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
The page that you see in
has been precompiled into a
stored in a JAR file
in the ROOT webapp’s
Because of this servlet,
Tomcat will not look at the contents of the ROOT web application’s
file if you change it.
The easiest way to change the contents of the
page is to remove this
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
To remove the
edit the ROOT web application’s configuration file,
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
servlet and restart Tomcat,
how does Tomcat know to compile the
index.jsp page in the ROOT web app’s directory?
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
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
which means Tomcat will try to load that file (if found) in order to display it.
configuration file also defines a servlet called simply
This section of the
<!-- 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>
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,
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
I’ve added the above instructions to the Tomcat HowTo wiki in the hope it helps Tomcat newcomers find their way around the server.
- 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.