aspose file tools*
The moose likes JSP and the fly likes getRealPath Depricated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "getRealPath Depricated?" Watch "getRealPath Depricated?" New topic
Author

getRealPath Depricated?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Hi,

If you need the full path to a directory under the Web Root, how do you do that without getRealPath()?

Creating a file with f = new FileOutputStream() will fail without a full path.

In my testing a path like \webroot\somedir\somefile.txt will fail.

I'm in a JSF page so I am currently using:

HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

to get to the request scope, but I'm sure this applies to JSP as well.

Any help would be appreciated.

Thanks!!!

-- Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

but I'm sure this applies to JSP as well


Nope. No such thing as FacesContext outside of JSF.

But to answer your original question, check out the methods on ServletContext.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Sorry, my question must have not have been clear.

I was trying to ask what I would use instead of getRealPath() that would give me a full path to be able to create a file (now that getRealPath() is depricated).

Hope this is clear.

Thanks.

-- M
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

First, getRealPath isn't going anywhere.
The notes in request.getRealPath (the deprecated one) tell you to use ServletContext.getRealPath which isn't deprecated.
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getRealPath(java.lang.String)

Regardless of whether it's deprecated or not, it's not a great idea to rely on it. There can only be a path to a resource in a webapp if the app is being run from an exploded file system. If the application is being run from a packed war file, getRealPath will return null.

I've always felt it's better to make the path to any resources you want to write to configurable via context or servlet init-params. For read-only resources, it's better to rely on Request.getResourceAsStream
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
TEST TEST TEST


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Hi Ben,

Hmmm, doesn't work...

If I change

HttpServletRequest request =
(HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

String webRoot = request.getRealPath("/");


to:


ServletContext request =
(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getRequest();

String webRoot = request.getRealPath("/");

==============

And, then try to create a FileOutputStream that's located on the Web Root, it fails with a FileNotFound Exception.

Using the full path of the first example above succeeds.

Is there a programmatic solution for this?

Were you suggesting to hard-code full path in an init-param?

Thanks again!!!

-- Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Since you are using JSF interfaces this would be more appropriate in the JSF forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getRealPath Depricated?