Edit the default JSP home page of Tomcat

Posted by jcargoo | Monday, November 10, 2008
| 0Delicious Twitter Reddit Digg Loading...


All the default Tomcat home page components come from the ROOT webapp servlet called org.apache.jsp.index_jsp. This page is simply $CATALINA_HOME/webapps/ROOT/index.jsp which 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.
Then the easiest way to change the contents of the index.jsp page is to remove

the 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.
But in order to remove correctly the index_jsp servlet, you can 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 the following:
<!-- 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?
First, when you request the default page of a web application, Tomcat 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.




How to encourage this blog if you like it:
  • Promote our sponsors;
  • Add any kind of comment or critic;
  • Ask me directly by email if you prefer.
Just do something like that, and I will have the huge pleasure to continue posting the best of the creativity I have.




Share this post ?

Digg Reddit Stumble Delicious Technorati Twitter Facebook

0 Previous Comments