Display Project/Directory listings in Tomcat Home Page

How to display a list of all applications deployed inside the Tomcat Webapps folder

By default the ROOT application index file in Tomcat has the Tomcat Home Page. But if you want to display a List of all the applications hosted in the webapps folder for that Tomcat Instance then create a project named ROOT in eclipse and change the index.jsp to the following code. After that just Export a WAR file named ROOT.war into the webapps directory.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Your application Title</title>

</head>
<body>
<div id="allContent">
<ul>
    <%
    String root = getServletContext()
        .getRealPath(File.separator)
        .replace("/ROOT","")
        .replace("\\ROOT","");
    String ignored = "ROOT";
    java.io.File file;
    java.io.File dir = new java.io.File(root);
    
    String[] list = dir.list();

    for (int i = 0; i < list.length; i++) {
        file = new java.io.File(root + list[i]);
        if (file.isDirectory() &&
            !list[i].equals(ignored)
            ) {
        %>
            <li>
             <h3>
             <a href="/<%=list[i]%>/" target="_top"><%=list[i]%></a>
             </h3> 
            </li>
        <%
        }
    }
    %>
</ul>
</div>
</body>
</html>

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.