wood burning stoves 2.0*
The moose likes Servlets and the fly likes getting the contextpath to read and write text files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting the contextpath to read and write text files" Watch "getting the contextpath to read and write text files" New topic
Author

getting the contextpath to read and write text files

Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,

First I have posted my query here in tomcat section, but I think this would be the right forum to ask

http://www.coderanch.com/t/84787/Tomcat/reading-text-files

My problem:
I have my java class files in tomcat

webapps/applicationName/JavaFiles/classes/JavaHelper.class

Text File folder is in

webapps/webResources/textFiles/*.txt

I need to get my helper java class to read and write the text file in 'textFiles' folder?

so from Tomcat section I got the response to write a servlet and get the context path. I don't have requests for the servlets from a webpage,

how can a java class request a servlet?



1)Can this be possible?

2)Is there any other way to do this without servlets?

Thanks.
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
If reading a text file from a servlet is your concern, this is for you

http://www.jguru.com/faq/view.jsp?EID=11569

Cheers
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
I need to read and write from a java class which is inside Tomcat's webapps folder. (I have folder structure in my first post)

I wrote teh servlet to get the context path, but not sure how to use it.. because I need to call this servlet from a class file not a web page.

thanks,
Eugene Lucash
Ranch Hand

Joined: Feb 19, 2005
Posts: 77
ServletContext.getRealPath(String)
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,

I tried this..



my Class file is in :

tomcatHOme\webapps\myServices\WEB-INF\classes\fileWriterTest\

I have
\Tomcat 5.5\webapps\myServices\WEB-INF\myTest\xml\defaultXmlFiles\

when i run the FileWrtierClass from command line it works fine.

but when I call this class from another application. it throws file not found error.

it should be something simple I am missing here..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Trying to find a file to write to dynamically is not a best practice.

While some app servers will unpack a war file into a directory structure, others will not. getRealPath(String) will return null if your app is being run from a war file (as opposed to an exploded directory structure).

You're better off configuring an absolute path as a servlet init param or a context init param.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bhiku Mhatre
Ranch Hand

Joined: Apr 08, 2002
Posts: 127
Hi Vani,
File myFile = new File("..//myTest//xml//defaultXmlFiles",fileName); is not the right way to do it. You are mapping a relative path. But in this case not relative to root context of the web app but relative to tomcat root directory.
You have following solutions.
1) Provide absolute path..(Hardcoding ..Bad idea)
2)Calibrate the path WRT tomcat root. (Tomcat specific Hardcoding .. Bad idea.)
3) Use ServletContext.getRealPath("/webResources/textFiles/*.txt") (Should work in all web containers.)

Regards,
Milind.


The difference between winner and loser is making things happen and letting things happen.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

3) Use ServletContext.getRealPath("/webResources/textFiles/*.txt") (Should work in all web containers.)

This will not work in all containers.
The servlet spec requires that apps be able to be run from a war file. It doesn't require that appservers unpack the war file into a directory structure on file system. When apps are run from a war file (not unpacked), getRealPath will return null.

The most dependable solution you will come up with is to use an absolute path, configured as either a servlet init param or a context init param.

Also: if this path points to a directory in your webapp and you are running the app in a packed war file, you will also run into a problem.

Another option is to use the 'tempdir' provided to each app by the container.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Thanks for your replies.

I don't have war files.

How would I give init param or context param to a servlet?

How would I call this servlet from java class instead of webpage?

thanks again,
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Take a look at "Servlet Init Params" at http://simple.souther.us.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
how could this init param help to get the context path?
[code]
<servlet>
<servlet-name>MyServlet</servlet-name>
<display-name>MyServlet</display-name>

<init-param>
<param-name>path</param-name>
<param-value>//myTestFile/xml/defaultXmlFiles</param-value>
</init-param>

<description>Simple text servlet</description>
<servlet-class>image_gallery.MyServlet</servlet-class>
</servlet>
[code]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

My point was that you shouldn't write apps that depend on being able to find a particular directory based on the location of the context because, depending, on how your app is run, the context may not have a physical location on the file system.
 
Don't get me started about those stupid light bulbs.
 
subject: getting the contextpath to read and write text files
 
Similar Threads
Servlets classes not compiling!.
package issue
Problems with the Example from HF Servlets & JSP in Chapter 3
Start In Servlets
HTTP Status 404 The requested resource () is not available