Skip to content

Instantly share code, notes, and snippets.

@hfknight
Created June 4, 2013 16:36
Show Gist options
  • Save hfknight/5707408 to your computer and use it in GitHub Desktop.
Save hfknight/5707408 to your computer and use it in GitHub Desktop.
Customize Tomcat Directory Listings
1. in tomcat/conf/server.xml, add the directory to appBase paramter in Host section
<Host name="test.example.com" appBase="/mnt/example" autoDeploy="true" reloadable="true">
2. in the listing directory, add WEB-INF/ folder, add web.xml in WEB-INF w/ the following:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Snapshot Browser</display-name>
<description>File Browsing Application for Site Snapshots</description>
<!-- Enable directory listings by overriding the server default web.xml -->
<!-- definition for the default servlet -->
<servlet>
<servlet-name>DefaultServletOverride</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>globalXsltFile</param-name>
<param-value>/root/scripts/example-listing.xslt</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Add a mapping for our new default servlet -->
<servlet-mapping>
<servlet-name>DefaultServletOverride</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3. create /root/scripts/example-listing.xslt file, do customization here:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xhtml" encoding="iso-8859-1" indent="no"/>
<xsl:template match="listing">
<html>
<head>
<title>
Sample Directory Listing For
<xsl:value-of select="@directory"/>
</title>
<style>
h1{color : white;background-color : #0086b2;}
h3{color : white;background-color : #0086b2;}
body{font-family : sans-serif,Arial,Tahoma;
color : black;background-color : white;}
b{color : white;background-color : #0086b2;}
a{color : black;} HR{color : #0086b2;}
</style>
</head>
<body>
<h1>Sample Directory Listing For
<xsl:value-of select="@directory"/>
</h1>
<hr size="1" />
<table cellspacing="0"
width="100%"
cellpadding="5"
align="center">
<tr>
<th align="left">Filename</th>
<th align="center">Size</th>
<th align="right">Last Modified</th>
</tr>
<xsl:apply-templates select="entries"/>
</table>
<xsl:apply-templates select="readme"/>
<hr size="1" />
<h3>Apache Tomcat/6.0</h3>
</body>
</html>
</xsl:template>
<xsl:template match="entries">
<xsl:apply-templates select="entry"/>
</xsl:template>
<xsl:template match="readme">
<hr size="1" />
<pre><xsl:apply-templates/></pre>
</xsl:template>
<xsl:template match="entry">
<tr>
<td align="left">
<xsl:variable name="urlPath" select="@urlPath"/>
<a href="{$urlPath}">
<tt><xsl:apply-templates/></tt>
</a>
</td>
<td align="right">
<tt><xsl:value-of select="@size"/></tt>
</td>
<td align="right">
<tt><xsl:value-of select="@date"/></tt>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
@cptdb
Copy link

cptdb commented May 3, 2017

Any suggestions on how to customize the display-timezone of the "Last Modified" column..? (Without changing Tomcat's underlying timezone itself..) Thanks a ton.

@rdp
Copy link

rdp commented Mar 16, 2022

I had to change servlet name to "default" then dir listings worked for followers...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment