JSP Navigation using Directories
- November 19th, 2009
- Write comment
This is not particularly hard or something you will want to do with every site you ever develop. That being said i recently used this same technique to list and make links of all of my web apps within a Tomcat configuration I have. First things first you will need to be running a platform capable of parsing JSP files for this to work and it is always a good idea to have your Java up to date currently I’m running 1.6.
The Scenario
We are developing an application that will check, and report back which applications are installed upon our Tomcat install. Further more we are ( having made sure that each web application has, in there root directory either an index or default page) going to create links to those pages.
ROOT
I am using the ROOT directory within the webapps directory, for the base of my application. I have emptied this directory and in its place put an empty index.jsp file.
at the top of the file is the usual language declaration,
<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
below this we are going to place an empty function that we will write later, this function will deal with the creation of the links.
<%!
public String createLink(String dir){
}
%>
As you can see from the above declaration the method will expect a String and return one also. The string being passed in will be the directory name. The returned will be the link in its fully formed and valid format.
Getting The Directory Contents
To do this we need a file object, a String array and a String. We place the relative location of the webapps directory within our string. Then call upon and instantiate the Java File object like so.
String filename=”..\\webapps\\”;
java.io.File file = new java.io.File(filename);
This code can be placed just before the last %> of the previous code section. this code takes the java.io.File object and creates a new instance of itself using the String supplied in this case the location of the webapps directory. We name this new File object file. We now take the contents of that directory and use the list method with the file object itself, to populate the String array.
String [] dirs = file.list();
We now have all the information we need to pass the Strings to the createLink function.
The Loop
To pass the individual strings into the createLink function we will loop through the array like so.
<ul>
<%
for(int i = 0; i < dirs.length; i++){
// if directory we are in skip it
if(dirs[i].equals(“ROOT”)){
continue;
}
String newLink = createLink(dirs[i]);
out.println(newLink);
}
%>
</ul>
You can see that there is a conditional check within the loop. If you remember we jumped out one directory with the file object so in affect we would have created a link to our own app. I chose not to but if you wish to have your own app listed simply remove this if statement block.
So now we need to create the function to create the links, I hope you have noticed that the separation is not totally necessary depending upon what you require you could quite easily fill the loop with this code and list out the directories. I have other purposes and other checks that needed to be done so I decided to put all these in a separate function. The code of which is here:
String link = “”;
link = “<li><a href=\”../” + dir + “\”>” + dir + “</a></li>”;
return link;
This code should be placed within the function block we created at the beginning. and that’s it all done with one reminder. This is very basic and should be used as such there has been very limited testing on this and most of the code was written in not copy and pasted in so please check for syntax errors especially in the ” ” areas.
