This discussion is archived
11 Replies Latest reply: Apr 16, 2012 5:36 AM by EJP RSS

Servlet home directory

924329 Newbie
Currently Being Moderated
Hello,

I am trying to create file using java servlet, but i don't know where it is located after it is created.
i tried to get the path and i get this path : /usr/local/jakarta/apache-tomcat-5.5.28/bin/hishamww.html

But i didn't find the file hishamww.html which i was trying to create it.

I tried to get the home directory in which my servlet is running and i got the following: /home/tomcat

and i searched for this directory but i didn't find it on my server!

This is the code that i am using to create the file:

**************************************************************************
File file = new File("hishamww.html");

if (file.createNewFile()) {
processRequest(request, response,"file created:"+file.getAbsolutePath()); //This gave me this path: /usr/local/jakarta/apache-tomcat-5.5.28/bin/hishamww.html
} else {

}

FileWriter fstream = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fstream);
out.write("hisham");
out.close();
***************************************************************************

I want to create this file in a directory that i can access in order to get it by http request like: www.mySite.com/fileName.html

Regards,
Hisham
  • 1. Re: Servlet home directory
    EJP Guru
    Currently Being Moderated
    I get this path : /usr/local/jakarta/apache-tomcat-5.5.28/bin/hishamww.html

    But i didn't find the file hishamww.html which i was trying to create it.
    You mean that file wasn't in that path?

    Anyway you need to look up HttpServletRequest.getRealPath().
  • 2. Re: Servlet home directory
    924329 Newbie
    Currently Being Moderated
    No, i mean that the file is in that path, but i cannot find that path in my server!

    There is no folder called usr in order to get access to its contents.
    I think that this path is the one that the servlet is running in, but what i want to do is to create a file in a directory that i can have access to it.
  • 3. Re: Servlet home directory
    EJP Guru
    Currently Being Moderated
    No, i mean that the file is in that path, but i cannot find that path in my server!
    You may not have access to the directory.
    There is no folder called usr in order to get access to its contents.
    Or you may not have access to the directory.
    I think that this path is the one that the servlet is running in
    So it exists but tyou may not have access
    but what i want to do is to create a file in a directory that i can have access to it.
    So see the API I referred you to. What you want to do is get the real path of a file called "/hishamww.html", which is a path within your webapp: maybe you want to add some directories within your webapp on top of that; then get the real path of that web-app-relative path via the API I mentioned; then proceed.
  • 4. Re: Servlet home directory
    924329 Newbie
    Currently Being Moderated
    I tried to get real path as follows:

    ***************************************************
    ServletContext context = getServletContext();
    String name = context.getRealPath("/hishamww.html");
    System.out.println("file path: " + name);
    processRequest(request, response,name);
    ***************************************************

    The output was: file path: /home2/ziadbu/public_html/hishamww.html

    then i tried to enter www.mySite.com/home2/ziadbu/public_html/hishamww.html but i got 404 error which is page not found.

    Any advice please.
  • 5. Re: Servlet home directory
    EJP Guru
    Currently Being Moderated
    String name = context.getRealPath("/hishamww.html");
    So that is the real file system name of the resource whose partial URL is /hishamww.html within your application context.
    The output was: file path: /home2/ziadbu/public_html/hishamww.html
    So that's where the file is on the disk.
    then i tried to enter www.mySite.com/home2/ziadbu/public_html/hishamww.html but i got 404 error which is page not found.
    Because it makes zero sense. You have addded the real path of the file to a hostname, and you are now expecting that to magically behave as a URL. It isn't a URL. It is just nonsense. The correct URL for that resource is http://www.mySite.com/myApplicationName/hishamww.html, whatever your application context name actually is.
  • 6. Re: Servlet home directory
    924329 Newbie
    Currently Being Moderated
    Thank you for your response,

    EJP wrote:http://www.mySite.com/myApplicationName/hishamww.html
    What do you mean by "myApplicationName", do you mean that it is the name of the servlet that i am running? or tomcat?, because i tried to write the name of the servlet that i am running but i got 404 error.
  • 7. Re: Servlet home directory
    gimbal2 Guru
    Currently Being Moderated
    my application name. Do you really need to ask?


    Of course you may have your application setup as the root webapp, in which case the URL can be:

    http://www.mySite.com/hishamww.html
  • 8. Re: Servlet home directory
    924329 Newbie
    Currently Being Moderated
    No its not the case!!!

    tomcat is setup on front of default web server which is default http server, so its not the root directory
  • 9. Re: Servlet home directory
    EJP Guru
    Currently Being Moderated
    What do you mean by "myApplicationName", do you mean that it is the name of the servlet that i am running?
    What on earth do you think I mean? Is it ambiguous? Isn't it blindingly obvious?
    or tomcat?
    Tomcat doesn't have a name. So it can't be that, can it? Not much left, is there?
    because i tried to write the name of the servlet that i am running but i got 404 error.
    Sigh. OK. Tell us:

    1. The hostname and port of Tomcat.
    2. The context name, i.e. the name of the application in Tomcat, i.e. the name of its .war file without the .war, or the name of the directory under webapps in which it is deployed.
    3. The name of the file.

    Then I will tell you the correct URL of the file which you have created via getRealPath("/hishamww.html");

    In fact I will tell you anyway. It is http://hostname:port/application/hishamww.html. Where you have to make the appropriate substitutions as above.
  • 10. Re: Servlet home directory
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    In fact I will tell you anyway. It is http://hostname:port/application/hishamww.html. Where you have to make the appropriate substitutions as above.
    Hmm, don't you mean this:

    http://hostname:port/contextname/hishamww.html

    ? If you don't do anything than the "application" name (IE the war filename without extension) will indeed be the context name, but it can be overridden through a context.xml file which many cut/copy/paste tutorials on the net will instruct you to do. heck, it can even be overridden from the IDE. By default the Eclipse WTP deployer will actually force the project name :/
  • 11. Re: Servlet home directory
    EJP Guru
    Currently Being Moderated
    Sure, context name.

    Just not 'Tomcat name', OK? ;-)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points