Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getRealPath Depricated?

 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are using JSF interfaces this would be more appropriate in the JSF forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic