wood burning stoves 2.0*
The moose likes Servlets and the fly likes Creating Directories using Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Creating Directories using Servlet

J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

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

Joined: Jul 26, 2007
Posts: 10170
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

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

Joined: Feb 05, 2004
Posts: 104

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.


Website: Learn Java by Examples
J Das
Ranch Hand

Joined: Jul 27, 2011
Posts: 83

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

Joined: Feb 05, 2004
Posts: 104

You can access it directly from your servlet using the getServletContext() method.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Directories using Servlet
 
Similar Threads
package com.example.model.* does not exist ERROR head first servlets
Filewriter File permission
Setting a filename filter in Filedialog
creating a jar for nokia sdk
Discrepancy in listing of a file using Windows Explorer and JAVA I/O classes