Archive for the ‘Java’ Category

JSP Navigation using Directories

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.

Java Recursive directory listings

A simple and yet troublesome for some, part of Java coding, is the ability to move recursively through directory structures. When processing directories full of files this is most definitely needed. We can accomplish this with a simple logic jump. We need to create a method to process all the files of a given directory, that when a directory is encountered calls itself passing in the new File that contains the directory.

The Test Directories

So to start out we need a directory to test with. I created a C:/test directory with example1.txt and example2.txt files. I then created the directory C:/test/exampledir and within that directory example3.txt and example4.txt.

The Main Method
package recurse;public static void main(String[] args){public class Main {// Pass in the test directoryExample.recurse("C:/test");}}

From the code above you can see we are calling a class named Example and within that class a method named recurse, and into that method we are passing a the directory path as a String.

The Example Class

package recurse;

import java.io.File;

public class Example {

public static void recurse(String dir){

}

At the start you can see that I have made this method have a return type of void this is because we are not going to pass back any information. this can easily be changed but is out of the scope of this tutorial. So lets set about collecting and dealing with the Files, firstly we need to store the directory we have just passed in.
// create file object for directory
File directory = new File(dir);

We do this in a File object, we now need to list all the files within that directory, but we need somewhere to store them. Creating a String array here is probably the simplest answer.

// get list of files from directory into a String array
String[] files = directory.list()

now with all the files in that directory contained within the String array we are set. I would like to pause here and make a very important point that may have already occurred to you, but is easily missed. The File object is not only capable of containing files but directories too, this point is very important to remember as we continue.

The Loop

We are at the pivotal point. Now we must loop through the files contained within the String array and determine what we need to do with each file.

// loop through files
for(int i = 0; i < files.length; i++){
// place current file string within a file object
procFile = new File(directory,files[i]);
// use file object to check if current file is a directory
if(procFile.isDirectory()){
// if file is a directory recurse
recurse(procFile.getPath());
}else{
// print out file name
System.out.println("The current file: " + procFile.getPath());
}
}

We can now assign the latest entry to be called by the loop to the procFile File object. then we test to see if procFile is a directory, if it is we call our self passing in procFile which will become the directory at the start of the method, allowing us to drop down and eventually crawl back up the tree directory structure. If however the procFile was not a directory its path is printed out.

Hope this helps let me know if you have any questions.

Java Assignment 3 update

OK its been a while since I have blogged this has been for many reasons the sudden on rush of assignments, and the realisation that I have to get them done quickly lol.

The news on the assignment is really good (I think anyway) its going really well my knowledge of Java Servlets and JSP pages is getting better all the time. The other person (who will be known as design guru from here on in) is really getting there teeth into the design and also understands the passion I have (some Say OCD) for getting programming done to an industry standard, whether I am actually at an industry standard will be decided when I start my placement I guess :-) .

We decided that in order to stand out and be a little different we would do a site that incorporates many challenges, which the least of which would be an inverted search indexing system for all items uploaded to the applications database. (wow that sounds a lot nastier than it should).

The part I am having real fun with is the Servlets that handle ll the major functions of the site, including the uploading of items, creation of new users and of course searching the database for appropriate images according to terms that are provided by the user.

I am trying to prevent SQL Injection hacks which I suppose due to the fact my SQL statements are prepared I may be OK (if I’m wrong let me know) I check the variables that are added to these SQL statements using regular expression also, before I get a lot of mail telling me I am stupid ;-) .

So what is it I hear you call ;-) its a stock images upload site that allows users to upload and sell there images for download, the site also offers credit tracking facilities. (well it will when I have finished that section lol).

Java Assignment 3

We have been presented with the specification for our third and final Java assignment. We are to design a fully dynamic web site utilising JSP, JavaBeans and servlets. This will be the most challenging part of the Java module so far. We are in groups of two and We have decided that we are going to attempt to create a stock photo web site. this is ambitious but also well within our grasp given the confidence we have for developing dynamic web pages.

Further updates will follow.

Return top