• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet read write location

 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a running servlet I would like to read and write to a file in the location where the servlet is running. I have not been able to find a method that gives me that location. I have tried the following functions and they are not giving me the information I need. Could someone help me on what I am missing.

contexString = request.getContextPath();
realString = request.getRealPath(contexString);
servletPathString = request.getServletPath();
servletPathInfoString = request.getPathInfo();

Thanks for your help in advance.
WR
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the location where the servlet is running


This is not a concept that has meaning. There is no "location" associated with a servlet instance.

What are you actually trying to accomplish?
 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I created a file and placed some text in the file I found the file in the Tom Cat directory. I don't want the file there. I would like to control the location of where the file is placed. My thoughts are if I know where the servlet is running then I can decide where to place the file as an offset of the location where the servlet class file is. I hope that makes sense.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find any location in your web application hiearchy through the use of the getRealPath() method of the ServeltContext (assuming you are not running the app out of an unexpanded war file).
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this work for you:

InputStream ServletContext.getResourceAsStream(String path)

?

The path must begin with a "/" and is interpreted as relative to the current context root.
 
mamidi subrahmanyam
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rouse,
I think you can use this �

...
...

File file = new File(�YouFileName�);
String location = file. getAbsolutePath();
System.out.println(location);
//Then you would know about the location where it is stored.
...
...
...
 
Satish SN
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rouse,

It is not at all a good idea to create the files in webserver in which servlet runs instead u should decide the location where these files can be created on the server where this application is hosted in that way u have more control over the files
 
William Rouse
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all. I have had a lot of fun. I have to rethink this all.
WR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic