Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

recursive function usage in JSP  RSS feed

 
Shreenivaas Reddy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All users here...

When we give a word in Search option of Windows...it displays the links to the specified file or Directory..isn't it!
I need to display the same in browser(atleast the absolute paths if not links)...for this I wrote code as shown below..with Im able to get the matching files which are in the root directory...but im not getting from subdirectories...
If i do the same normal java class im getting the required output...
pls help me out.

<html>
<body>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%!

public List lFiles(String dir)
{
File dn = new File(dir);
ArrayList list=new ArrayList();
File fList[] = dn.listFiles();
String str="";
String s="xyz.txt";
for(int i=0;i<fList.length;i++){

if(fList[i].isDirectory())
{

lFiles(fList[i].getAbsolutePath());
}
else

if((fList[i].getName()).equals(s)){
list.add(fList[i].getAbsolutePath());
System.out.println(list);
}
}
return list;
}
%>
<%
File f[] = File.listRoots();
for(int i=3;i <4;i++){
String dir = f[i].getAbsolutePath();
%>
<%=lFiles(dir) %>
<%
}
%>
[ July 05, 2006: Message edited by: Bear Bibeault ]
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are printing all files, recursively, so the system.out.println is the correct result, if you wanted to see your files in a depth first traversal of your directory structure.
But the list object you are building is not being captured for recursive calls to the method, and it is only returning the first invocation to lFiles.
So, we need to have the recursive function populate the same list object with each recursive call,

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shreenivaas Reddy
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular, please see EaseUp

It explains why adding "urgent" to your subject line usually results in slower responses in user forums such as this.

Also:
If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've adjusted the topic title.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!