input files

Hi there,

I'm trying to read in an input file with a jsp file. The reading itself is done by a class that is imported by the jsp file.

Now this all works fine, but the problem is that he can't find the specific input file

Does anyone now where I have to place the input file on the server (using tomcat5.5) .

I tried to place it in the same directory where I strored the .class file that wants to read it. Unfortunenately it it's not working

I hope someone really can help me out.

thank you in advance

Message was edited by:

mol-mol

[595 byte] By [mol-mola] at [2007-10-3 0:54:17]
# 1

The path of files differs depending on whether you access the the class directly (through a compiler or command line), or through Tomcat.

You may find the following links useful, I used the first one to solve this problem by printing out the user.home property, telling me what the current working directory was. Through this i realised my classes were operating out of different locations, depending on whether I instantiated the class through my compiler, or through Tomcat.

Another tip I would offer is to use the file.separator and path.separator, properties instead of hard coding, that way, if you move to a different operating system your app will still work!!

http://www.scit.wlv.ac.uk/appdocs/java/api/java/lang/System.html

http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/util/ProcessEnvironment.html

angrycata at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

The best option is to use the ServletContext's getResourceAsStream() method. And the context begins with a "/". So if you have placed your file in say the WEB-INF directory and your file name is 'inputFile.something', then

InputStream is = context.getResourceAsStream("/WEB-INF/inputFile.something");

returns an input stream object that you can wrap around say a BufferedReader to read the file.

If it was placed in a folder called 'myFiles' which is on the top level (the same level as your WEB-INF) directory, you would

InputStream is = context.getResourceAsStream("/myFiles/inputFile.something");

and if the myFiles directory was a step below, say under the WEB-INF directory, then use

InputStream is = context.getResourceAsStream("/WEB-INF/myFiles/inputFile.something");

The catch in all of the above is that you'll have to have access to the ServletContext object. This is readily available in a servlet using the getServletContext() method or in a jsp as the application object.

In other classes, you'll have to pass the context object along after having retrieved them from a Servlet or a jsp.

ram.

Madathil_Prasada at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi Ram,

Is it possible to access these values directly from the class.

When using connection pooling I used

try

{

this.ctx = new InitialContext();

this.ds = ( DataSource ) ctx.lookup( "java:comp/env/jdbc/TestDB" );

}

To access a connection pool, can the same method be used here, making the class less dependant on jsp?

angrycata at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
I am not sure that I understand you correctly.Are you suggesting that you use the jndi context to look up the files (just like how you used jndi to retrieve a handle to the connection pool)?ram.
Madathil_Prasada at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi ram,/WEB-INF/filename, I think doesn't really fetch the file u actually want to access.Isn't it /Application/WEB-INF/filename?Correct me if I'm wrong.Cheers Mouli
mouli_indiaa at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hi mol-mol,I think the problem is with the file path.Get "real path" of the file, and try to read it. I hope it'll work:-)Raj
newentrya at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

> I am not sure that I understand you correctly.

>

> Are you suggesting that you use the jndi context to

> look up the files (just like how you used jndi to

> retrieve a handle to the connection pool)?

>

> ram.

No, just to look up Tomcats working directory.

angrycata at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
BTW, I put read in files in thier own separate directory under the web-app (the root of your website) folder
angrycata at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

> > I am not sure that I understand you correctly.

> >

> > Are you suggesting that you use the jndi context

> to

> > look up the files (just like how you used jndi to

> > retrieve a handle to the connection pool)?

> >

> > ram.

>

> No, just to look up Tomcats working directory.

Use jndi to look up the connection pool - remember that is an object (a collection of connections) bound to a registry accessible via jndi.

Looking up a file is different and there are different ways to achieve it. I would recommend the method in the ServletContext object. But sure there are other ways.

ram.

Madathil_Prasada at 2007-7-14 17:49:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...