• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting the contextpath to read and write text files

 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

https://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.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ServletContext.getRealPath(String)
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at "Servlet Init Params" at http://simple.souther.us.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic