tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Smith <d...@cornell.edu>
Subject Re: accessing files/dirs.....
Date Wed, 04 Apr 2007 17:39:50 GMT
What I posted was an excerpt from a jsp page writing the index of a 
folder -- no Struts in sight.  "directory" was just an arbitrary request 
parameter for example purposes:

http://localhost:8080/myWebApp/imageList.jsp?directory=%2fsome%2fdirectory

It doesn't really matter how you get that string.  In your case, you 
don't even need the for loop -- just write the count or store it in the 
pageContext for later use in a tag like <c:out value="${fileCount}" />

For getting the count of files, just replace the contents of the if 
(dirFile.isDirectory()) block with this one statement:

pageContext.setAttribute( "fileCount", dirFile.list().length ) ;

And then you can write it out later using the c:out tag as above or 
<%=pageContext.getAttribute( "fileCount" )%>

--David


maya wrote:

>
> yes I know, eventually I want to do this whole webapp in struts..  for 
> now just want to be able to count how many images in 'images' folder..
>
> (what I don't get here is first line..  where does the reqParameter 
> come from?  (this method is usu. used to get params from request (user 
> input, query string, etc..)  so don't get where "directory" is coming 
> from here..)
>
> thank you very much..
>
>
>
> David Smith wrote:
>
>> Ok... I think I'm starting to see the picture here.  You want to be 
>> able to write a jsp that can list the directory contents of a 
>> folder.   This is very crude, no error checking, untested, etc. but 
>> you'll catch on....
>>
>>    <ul>
>>    <%
>>      String reqParameter = request.getParameter( "directory" ) ;
>>      URI dirURI = this.getServletContext().getResource( reqParameter 
>> ).toURI() ;
>>      File dirFile = new File( dirURI ) ;
>>      if ( dirFile.isDirectory() ) {
>>          String[] fileList = dirFile.list() ;
>>          for ( int index = 0; index < fileList.length; index++ ) {
>>              out.println( "<li>" + fileList[ index ] + "</li>" ) ;
>>          }
>>      }
>>    %>
>>    </ul>
>>   And it does comply with the servlet spec.
>>
>> --David
>>
>>
>>
>> maya wrote:
>>
>>>
>>> thank you..  someone in another forum said the same thing.. problem 
>>> is, if I have trouble accessing directory, how will I tell these 
>>> methods to "get Resource" for that dir????? :)
>>>
>>> (also, alas, don't quite remember how to use methods of interfaces 
>>> -- since interfaces can't be instantiated, didn't find getInstance() 
>>> method for this interface..  but well, can look this up elsewhere, I 
>>> suppose....:)
>>>
>>> thank you...
>>>
>>>
>>>
>>> David Smith wrote:
>>>
>>>> I'm not sure what you're attempting to do here, but have you 
>>>> thought about ServletContext.getResource() and 
>>>> ServletContext.getResourceAsStream() ?  Both are safe methods of 
>>>> reading resources from the webapp whether it be in a compressed 
>>>> archive or not.  There is also getRealPath(), but it will only work 
>>>> if the webapp is expanded (ie not a .war file).
>>>>
>>>> I can't recommend it enough -- read the servlet spec.  It really 
>>>> does help.
>>>>
>>>> --David
>>>>
>>>> maya wrote:
>>>>
>>>>> File dir = new File("C:\\apache-tomcat-5.5.17\\webapps\\india\\delhi\
>>>>> \images");
>>>>>
>>>>> this works fine in my machine locally, but on my website.. if, say,
>>>>> I'm in dir where 'images' dir is, this doesn't work...
>>>>>
>>>>>         File dir = new File("images")
>>>>>
>>>>> starting @ root of webapp also doesn't work...
>>>>>
>>>>>     File dir = new File("/india/delhi/images");
>>>>>
>>>>> names of directors are good, they are all lowercase...
>>>>>
>>>>> have conditional to test...
>>>>>
>>>>>             if (imgsList == null)
>>>>>                out.println("null");
>>>>>
>>>>> always prints 'null' on my website...
>>>>>
>>>>> do paths in this constructor have to be absolute?  so on my website
>>>>> path would have to start with "http..."?????
>>>>>
>>>>> is this an access problem?  if so, do I need to ask my webhosting 
>>>>> folks?  I know this is not a very "kosher" way of doing it, but am 
>>>>> running this code in a JSP for now (still don't know enough struts 
>>>>> and such to do this kind of stuff in a stand-alone class..  but of 
>>>>> course eventually will have to..  all I want to do is count how 
>>>>> many images are in 'images' dir.. that's it..)
>>>>>
>>>>> thank you...
>>>>>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message