How do I customize directory listings?

You can override DefaultServlet with you own implementation and use that in your web.xml declaration. If you can understand what was just said, we will assume you can read the code to DefaultServlet servlet and make the appropriate adjustments. (If not, then that method isn’t for you)

You can use either localXsltFile or globalXsltFile and DefaultServlet will create an xml document and run it through an xsl transformation based on the values provided in localXsltFile and globalXsltFile.localXsltFile is first checked, followed by globalXsltFile, then default behaviors takes place.

Format:

<listing>

<entries>

<entry type=’file|dir’ urlPath=’aPath’ size=’###’ date=’gmt date’>

fileName1

</entry>

<entry type=’file|dir’ urlPath=’aPath’ size=’###’ date=’gmt date’>

fileName2

</entry>

</entries>

<readme></readme>

</listing>

 

  • size will be missing if type=’dir’
  • Readme is a CDATA entry

 

The following is a sample xsl file which mimics the default tomcat behavior:

 

<?xml version=”1.0″ encoding=”UTF-8″?>

 

<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”

version=”3.0″>

 

<xsl:output method=”html” html-version=”5.0″

encoding=”UTF-8″ indent=”no”

doctype-system=”about:legacy-compat”/>

 

<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;}

table td { padding: 5px; }

</style>

</head>

<body>

<h1>Sample Directory Listing For

<xsl:value-of select=”@directory”/>

</h1>

<hr style=”height: 1px;” />

<table style=”width: 100%;”>

<tr>

<th style=”text-align: left;”>Filename</th>

<th style=”text-align: center;”>Size</th>

<th style=”text-align: right;”>Last Modified</th>

</tr>

<xsl:apply-templates select=”entries”/>

</table>

<xsl:apply-templates select=”readme”/>

<hr style=”height: 1px;” />

<h3>Apache Tomcat/<version-major-minor/></h3>

</body>

</html>

</xsl:template>

 

 

<xsl:template match=”entries”>

<xsl:apply-templates select=”entry”/>

</xsl:template>

 

<xsl:template match=”readme”>

<hr style=”height: 1px;” />

<pre><xsl:apply-templates/></pre>

</xsl:template>

 

<xsl:template match=”entry”>

<tr>

<td style=”text-align: left;”>

<xsl:variable name=”urlPath” select=”@urlPath”/>

<a href=”{$urlPath}”>

<pre><xsl:apply-templates/></pre>

</a>

</td>

<td style=”text-align: right;”>

<pre><xsl:value-of select=”@size”/></pre>

</td>

<td style=”text-align: right;”>

<pre><xsl:value-of select=”@date”/></pre>

</td>

</tr>

</xsl:template>

 

</xsl:stylesheet>

 

Sessions, Users, and Registration
Cookies

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?