Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
    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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Relative Address Problems

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

Ive got a servlet that needs a java.ser file, that sits on the same server as the servlet.

While I have been developing the servlet I have been using the absolute address of the file E:/Apache_Tomcat_5.5.20/webapps/mobileapplications/WEB-INF/lib/java.ser.

Now that I am ready to move it to a proper server rather than one I am running locally, I seem to be having some problems.

I know that I am going to need a relative address so I tried /mobileapplications/WEB-INF/lib/java.ser.

But this gives me a java.io.FileNotFoundException.

Can anyone know why this might be?

Can I do this method to find the file with Java?

Any help would be great.

Thanks.

Darren
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
First of all, putting in WEB-INF/lib, not so good an idea.

If it needs to be in the class path, put it in WEB-INF/classes.

But... it doesn't need to be so...

Put it in WEB-INF, or any folder under WEB-INF (but not lib or classes) and then use ServletContext.getResourceAsStream() to open it.
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That looks like the stuff I need.

However the next problem is that the class that actually uses the file is a normal Java class that is called by the Servlet.

Can I use the getResourceAsStream() method inside a normal class?

Do you know what Java API it is in off hand so I could have a look at it?

Thank you.

Darren
 
Bear Bibeault
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If the POJO is not web-aware, it should be passed the input stream as opened by the controlling servlet.
 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Darren Jackson:
Can I use the getResourceAsStream() method inside a normal class?

Well, you shouldn't have to. You would call that method in your servlet. It gives you an InputStream. Then you pass that InputStream to a method of that other class.

If that other class is expecting a File (or a filename) and won't accept an InputStream, then it should really be fixed so that it will accept an InputStream.
 
Bear Bibeault
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Failing that, you could instead, use ServletContext.getRealPath() to get the path to the file and pass that.*

And failing that, you could put the file in WEB-INF/classes and use the class loader's getResourceAsStream() implementation.

But I'd opt for having the POJO accept an InputStream as the most versatile.

*Assumes that your app is not running out of an unexpanded war file.
 
Bear Bibeault
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Paul and I bumped heads... but are in agreement on the best approach!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Darren,
In the future, please do not start new threads for the same issues.
It's very frustrating to spend time helping people only to see them asking the same question somewhere else.

Please continue this in the original thread
https://coderanch.com/t/364229/Servlets/java/Java-Serializable-Servlets
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic