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

Creating Directories using Servlet

 
J Das
Ranch Hand
Posts: 83
Firefox Browser Java MS IE
  • Mark post as helpful
  • send pies
  • Report post to moderator
hello everyone , please help me in creating directories using servlet. Somewhere over the net i saw the below code for creating folders :


but getRealPath is depreciated i guess.. so let me know what can i do.. Please reply me soon.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Whenever a method is deprecated, most of the times they suggest a work around. Did you check the API docs for suggested alternatives?
J Das wrote:Please reply me soon

Please EaseUp
 
J Das
Ranch Hand
Posts: 83
Firefox Browser Java MS IE
  • Mark post as helpful
  • send pies
  • Report post to moderator
Maneesh Godbole wrote:Whenever a method is deprecated, most of the times they suggest a work around. Did you check the API docs for suggested alternatives?

?
The API does not say it is depreciated. So can i use that code ?? Please explain me in details the path that i need to give within the getReaPath() ..
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,

I can give you a couple of note about this:

1. The HttpServletRequest.getRealPath() is deprecated, you can use the ServletContext.getRealPath() method as a replacement. It stated in the javadoc if you've read it.
2. Your app should be deployed in exploded mode, when you deploy the webapp in war format the getRealPaht() method return null.
3. When you creating multiple directory use File.mkdirs() instead of File.mkdir().

Hope this can help you.
 
J Das
Ranch Hand
Posts: 83
Firefox Browser Java MS IE
  • Mark post as helpful
  • send pies
  • Report post to moderator
I Wayan Saryada wrote:Hi,

I can give you a couple of note about this:

1. The HttpServletRequest.getRealPath() is deprecated, you can use the ServletContext.getRealPath() method as a replacement. It stated in the javadoc if you've read it.
2. Your app should be deployed in exploded mode, when you deploy the webapp in war format the getRealPaht() method return null.
3. When you creating multiple directory use File.mkdirs() instead of File.mkdir().

Hope this can help you.


Thank You for your suggestion Wayan.. But can you help me with the code for this ? I mean how should i use the ServletContext.getRealPath() method ? Give an example if possible.
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can access it directly from your servlet using the getServletContext() method.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Report post to moderator
How should you use it? Well, you shouldn't use it. Creating files inside your web application is a bad idea.

If they are meant to be permanent files, then don't put them in your web app, because they will disappear as soon as you redeploy it. Put them somewhere else.

And if they are meant to be temporary files, then Java already has a system property ("java.io.tmpdir") which contains the name of its temporary-file directory. Put them there.
 
    Bookmark Topic Watch Topic
  • New Topic