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

problem getting real path by request.getRealPath("\\")  RSS feed

 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
I am using tomcat 4.0
I have created a photo-album using jsp 2.0 & have deployed on linux red hat 9.
in index.jsp I used given code
<%
String Path=request.getRealPath("\\");
File someDirectory=new File(Path+"photos");
String[] photoList = someDirectory.list();
Arrays.sort(photoList);
%>


I have craetd a dir on under
home/vijay/photoalbum
----------------------photos [contains .jpg files]
----------------------WEB-INF
----------------------index.jsp
----------------------showphotos

in index.jsp photoList string arrays is showing null..

althoug the same dir structure & same code working smmothly on windows.
plz suggest me how can I get the realpath in linux..


thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65832
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Javasavvy",

You have been asked on at least one previous occasion to change your display name to conform to JavaRanch standards. This is not optional.

Visit the JavaRanch Naming Policy and adjust your display name to match it.

Please be aware that accounts with invalid display names are subject to closure.

bear
Forum Bartender
[ January 13, 2005: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getRealPath("/");
Always use the forward slash.

NOTE: Using getRealPath will return null when called in an app being deployed from a war file -- as opposed to an exploded file system. This means you're limiting the portablility of your app by relying on it.
[ January 14, 2005: Message edited by: Ben Souther ]
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben, is there a viable alternative to getRealPath when your app is in a packaged environment? I had this problem migrating code to a BEA environment.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For resources that you're going to package with your app (in your war file) you have servletContext.getResource and servletContext.getResourceAsStream.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html

If you want to work with files that get added after your app is deployed, like an upload directory, you could either make the location configurable with ServletInitParams or use the working/temp directory provided by your application server which you can find with:
I think that attribute is required in the specs but I'm not sure so you'll want to check the documentation for the server you're using. You'll also want to make sure that the server doesn't periodically clean that directory for you. Tomcat doesn't.
[ January 16, 2005: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of guessing, I looked it up.

SRV.3.7.1 Temporary Working Directories
A temporary storage directory is required for each servlet context. Servlet
containers must provide a private temporary directory for each servlet context, and
make it available via the javax.servlet.context.tempdir context attribute. The
objects associated with the attribute must be of type java.io.File.
The requirement recognizes a common convenience provided in many servlet
engine implementations. The container is not required to maintain the contents of
the temporary directory when the servlet container restarts, but is required to
ensure that the contents of the temporary directory of one servlet context is not
visible to the servlet contexts of other Web applications running on the servlet
container.



"The container is not required to maintain the contents of
the temporary directory when the servlet container restarts"

That is the important part. You should either read your server's documentation on this or perform some tests before relying on that directory for important files.

This is from Servlet Spec 2.4. If you server supports only a previous specification, you will want to look it up there as well.
[ January 16, 2005: Message edited by: Ben Souther ]
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow Ben thanks for the research. I'm using the tempdir currently for a caching location which works great. A for an upload location, it might be a little too unpredicatable. I suppose the best approach is to establish a directory outside of the app's context and upload/retrieve from there. Thanks for the info.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome.
I use a servlet init param to configure the location of the upload dir, myself.

I wouldn't exactly call it research ;-). I keep a copy of the servlet spec in the top right hand corner of my machine's desktop. The research involved clicking on it, and doing a text search for 'tempdir'.

Anyway, I think you're on the right track.

-Ben
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!