| Author |
Servlet read write location
|
William Rouse
Ranch Hand
Joined: Apr 12, 2006
Posts: 73
|
|
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
Joined: Jan 10, 2002
Posts: 56173
|
|
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?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
William Rouse
Ranch Hand
Joined: Apr 12, 2006
Posts: 73
|
|
|
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
Joined: Jan 10, 2002
Posts: 56173
|
|
|
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
Joined: Apr 10, 2006
Posts: 75
|
|
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.
|
It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
|
 |
mamidi subrahmanyam
Greenhorn
Joined: Jun 29, 2005
Posts: 18
|
|
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
Joined: Apr 19, 2005
Posts: 70
|
|
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
|
Satish SN<br />SCJP 1.4 & SCWCD 1.4
|
 |
William Rouse
Ranch Hand
Joined: Apr 12, 2006
Posts: 73
|
|
Thank you all. I have had a lot of fun. I have to rethink this all. WR
|
 |
 |
|
|
subject: Servlet read write location
|
|
|