Servlet: read/write from/to an ascii file

Hi,

I want to know if it is possible to read/write from/to a server local file, with servlet.

That is, I can simply do this to create a file and wrtie in it:

PrintStream ps =new PrintStream(new FileOutputStream(new File("test.txt")));

But in this case the file test.txt is placed in the main Tomcat directory.

But JSP hosting service give you permissions only on your account folder.

So Is it possible to create it in my account directory without use server full path?

How?

Thank you in advance

MargNat

Message was edited by:

MargNat

[732 byte] By [MargNata] at [2007-11-27 1:03:27]
# 1

> So Is it possible to create it in my account

> directory without use server full path?

No. But you can get the full path easily using ServletContext#getRealPath().

String rootPath = servletContext.getRealPath("/");

returns the root path of the current web container.

BalusCa at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Ok, I manage in upload a file through html form and save it where I want.

But I can't use it in order to load a file config.txt stored in myProject dir.

That is:

suppose I've created a project named myProject;

so I've a folder myProject with this structure:

myProject/

myProject/WEB-INF/

myProject/WEB-INF/classes/

myProject/WEB-INF/lib/

that is a standard JSP project directory.

JSP pages are in the myProject/ dir;

servlet class files are in the myProject/WEB-INF/classes/ dir;

file config.txt I want to read is in the myProject/WEB-INF/;

Now, if I do this in a servlet class, I can read the file:

public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

public TestServlet(){

super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doPost(request,response)

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ServletContext sc = this.getServletxContext();

String path = sc.getRealPath("\\");

BufferedReader br = new BufferedReader(

new InputStreamReader(

new FileInputStream(

new File(path))));

String url = br.readLine();

...

...

}

}

But the problem is I want to read the file from a non-servlet class (suppose a model class);

So, how must I do to call the getRealPath() method from a non-servlet class?

Thank you in advance.

MargNat

MargNata at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
No ideas?
MargNata at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

First, I recommend you to use "/" instead of "\\". Not only this reads better without the -for newbies confusing- escape sign, but the forward slash also works in UNIX systems, while the backslash doesn't.

Second, when you're about to access a file in a non-servlet class outside the servletcontext, then you've to define the path yourself.

This property might be useful to find out the root path of the current package:String projectRoot = System.getProperty("user.dir");

BalusCa at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Ok,

I know this property;

but, I can't use this property in order to load a file from WEB-INF/ directory.

That is, If you have a file in your WEB-INF dir, How do you do to load it from a non servlet class?

the user.dir property isn't a good way because it depends on the tomcat server configuration.

As example, I've used Eclipse for project creation;

If I use user.dir property and run the project in Eclipse, I obtain the path "c:\programs\eclipse";

but my tomcat installation is in "c:\programs\Apache software foundation\Tomcat 5.5\".

Now I've placed my file config.txt in "c:\programs\eclipse" and in "c:\programs\Apache software foundation\Tomcat 5.5\" so I can load it either when I run project by Eclipse or by Tomcat standard.

But on a JSP server I should ask to host manager to place the file config.txt in the main Tomcat directory.

And I don't want to have this.

Any alternative?

Thank you anyway.

MarNat

MargNata at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Ok,

I know this property;

but, I can't use this property in order to load a file from WEB-INF/ directory.

That is, If you have a file in your WEB-INF dir, How do you do to load it from a non servlet class?

the user.dir property isn't a good way because it depends on the tomcat server configuration.

As example, I've used Eclipse for project creation;

If I use user.dir property and run the project in Eclipse, I obtain the path "c:\programs\eclipse";

but my tomcat installation is in "c:\programs\Apache software foundation\Tomcat 5.5\".

Now I've placed my file config.txt in "c:\programs\eclipse" and in "c:\programs\Apache software foundation\Tomcat 5.5\" so I can load it either when I run project by Eclipse or by Tomcat standard.

But on a JSP server I should ask to host manager to place the file config.txt in the main Tomcat directory.

And I don't want to have this.

Any alternative?

Thank you anyway.

MargNat

MargNata at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

If you don't have a ServletContext available, then the best alternative is to put the file you want into the classpath, and use the ClassLoader method getResourceAsStream().

So move the file config.txt into the WEB-INF/classes directory

You can load it using

getClass().getClassLoader().getResourceAsStream("config.txt");

Cheers,

evnafets

evnafetsa at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Yes, it works!Thank you very much evnafets.I'll give you the duke.MargNat
MargNata at 2007-7-11 23:38:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...