• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WHY Doesnt this work in Tomcat and it does in Resin

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im moving my design site to a tomcat driven site. I have used this exact code a thousand times on Resin enabled servers and even on my local machine but tomcat ignores it. This is the text portion of my page and they are read from .txt files......everything else works just ducky except no text! I even tryed this on one of my other sites that runs resin and its flawless.....What the heck!!! Here is the code:
ServletContext sc = getServletContext();
InputStream is = sc.getResourceAsStream("/sitePages/"+pageName+".txt");
if (is != null){
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s;
while ((s = br.readLine()) != null){
out.println(s);
}
br.close();
is.close();
}
Any help would be much appreciated!!!
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:

It might illuminate you
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this (tested on Tomcat, but not Resin):

I, too, have been just starting to work with Tomcat (comes with Forte 3.0 Enterprise) after having worked with a simple syntax-highlighting editor and Resin, and I can say that Resin seems to be much easier to work with. Despite everyone raving about Tomcat, I'm much more impressed with Resin at this point, and I especially recommend it to anyone who's just starting to learn J2EE servlets/jsps. www.caucho.com
Let me know if the above
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well kids neither of these really helped. On the first reply with the foo.getAbsolutePath() I got /sitePages/home.txt. The second response wont even compile as DataInputStream in = new DataInputStream(buffer); is deprecated. Im tinkering with it now to see what I can come up with...
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Im justabout as frustarted as ive ever been in my life with this. Tomcat my &*(*$%..... I even tryed this code which gives me the absolute path and STILL the InputStream object is coming back as null! What the heck! Here's what i tried:

ServletContext sc = getServletContext();
String path = sc.getRealPath("/sitePages/"+pageName+".txt");
out.println(path);
InputStream is = sc.getResourceAsStream(path);
if (is != null){
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s;
while ((s = br.readLine()) != null){
out.println(s);
}
br.close();
is.close();
}
On the out.println(path) Im getting the path to the file (which ended up on the root of my server partition NOT in the web-inf/classes/sitePages folder like Resin returns) So I moved the folder and articles to where the path lead me....Still nothing. I also printed to the page the is object and it is returning null!..I just don't get this. Ive been doing this for months now on another site and have NEVER had a problem with it!
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following works on both Tomcat 3.2 server (came built-in to Forte for Java Enterprise 3.0) and Resin 2.0x, using JDK 1.3.1_01. Here's my file layout:
<doc-app-root>\
<doc-app-root>\sitePages\foo.txt
<doc-app-root>\WEB-INF\classes\FileTester.class
I suggest that you use relative paths and stay away from using getRealPath(). The code includes two methods to get the input stream. This code compiles without deprecation warnings, and executes flawlessly. If it doesn't work on yours, then I suggest either re-installing Tomcat (pick a version marked 'stable') or forgetting Tomcat altogether and going back to Resin.
 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all I got it working using a URL and then getting the context. Sorry I didnt write back sooner.......been real busy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic