This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to create a file on web server at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to create a file on web server at runtime" Watch "how to create a file on web server at runtime" New topic
Author

how to create a file on web server at runtime

utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi

i want to create and save a file in inside my web application directory which is runninig on server. How can i do that? How do I get the absolute path of my project directory? My web server is apache tomcat.

Please Help

Thanks
Utsav.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Check out ServletContext#getRealPath


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Do you really want to save the file inside your web app? Really? That means that when you redeploy the app, you need to either wipe out the created files, or restore them in some fashion. That's fragile and a royal pain. Better to store the files external to the web app.

But to answer the question, check out ServletContext.getRealPath()


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi

Thanks for the responses..

@Maneesh
i cannot use ServletContext#getRealPath as it is creating other problems for me. Also it requires a file as a parameter where as in my case the file has to be created and is not existing currently.

@Bear
Honsestly I would not like to keep the file in my web app folder but I am not sure if I would be allowed to write the file somewhere outside on a professional web server.
Secondly these files would be temporary and would be created on per request basis.

How is this problem solved in case of real world applications.. I mean what is the standard approach?

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Such files are usually written to the system temp folder.

And saying things like "I cannot use the correct approach because I had problems" is self-defeating and won't get you much of anywhere. In this case it's moot as writing into the web app is the wrong approach, but in the future don't dismiss what might turn out to be the correct way of doing things just because you do not understand how to use it yet.
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi Bear

- I had read somewhere that ServletContext.getRealPath is not a reliable approach to follow (though i dont know why )

Secondly I need to give a virtual file name as a parameter to this method what should be that? any file that is present in my web app?

Thirdly I tried using it like this
getServletContext().getRealPath("/web.xml")

but it gave me null pointer exception
please tell me if it is not the correct way..

Also the file that is creating the file on server is not a servlet so i'll have to pass this path as a parameter to that class whic I want to avoid.

Lastly I am new to web apps in java so please be easy on me

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

utsav gupta wrote:- I had read somewhere that ServletContext.getRealPath is not a reliable approach to follow (though i dont know why )

You may have heard this because:
  • It is frequently used inappropriately, as in this case.
  • It cannot be used if a web app is deployed as an unexpanded war file.


  • Secondly I need to give a virtual file name as a parameter to this method what should be that? any file that is present in my web app?

    No, why would you need to give it a file when you are looking for a folder?

    Thirdly I tried using it like this
    getServletContext().getRealPath("/web.xml")

    web.xml isn't stored in the context root, why would you look for it there?

    but it gave me null pointer exception

    No, it didn't. You likely got the NPE when you tried to use the result, which is likely to be null.

    Also the file that is creating the file on server is not a servlet so i'll have to pass this path as a parameter to that class whic I want to avoid.

    Why? You seem to have a habit of wanting to avoid the proper way of doing things.

    utsav gupta
    Ranch Hand

    Joined: Mar 19, 2009
    Posts: 57
    Hi

    i tried with

    System.out.println(getServletContext().getRealPath("/WEB-INF"));

    But its again givin NPE.

    Can you please tell as to how to use this method?.

    Also how am I supposed to tackle this when I am using an unexploded WAR file on server?

    Thanks
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Bear Bibeault wrote:Do you really want to save the file inside your web app? Really? That means that when you redeploy the app, you need to either wipe out the created files, or restore them in some fashion. That's fragile and a royal pain. Better to store the files external to the web app.


    And (as Bear also alluded to) it's even more of a pain when your web app is an unexploded WAR file. In fact, it's impossible to update that in any way, least of all to add new files to it. Face it, you're going to have to give up on that idea and consider doing it the right way.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60765
        
      65

    utsav gupta wrote:System.out.println(getServletContext().getRealPath("/WEB-INF"));

    But its again givin NPE.

    Sorry, but it is impossible for that line to yield an NPE. Are you sure that this is the line causing the exception?

    But as had already been pointed out: this is a pointless exercise. Just write the file to a temp folder and be done with it.
    utsav gupta
    Ranch Hand

    Joined: Mar 19, 2009
    Posts: 57
    Hi

    I understand that the right way to do it is to write the file in some temp folder on the server which is outside my application. But will I be allowed to do so on a rented server? If I am correct I will not have the permission to do that. Is that correct?

    Regards
    utsav.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60765
        
      65

    You could always ask.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to create a file on web server at runtime
     
    Similar Threads
    What do I need to run servlets?
    Is it possible to create a folder on the web server from a servlet?
    setting log4j
    I set up JBoss mail, so why can't I access the mail bean?
    getting file from web