Bookmark Topic Watch Topic
  • New Topic

Relative Address Problems  RSS feed

 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
If the POJO is not web-aware, it should be passed the input stream as opened by the controlling servlet.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Paul and I bumped heads... but are in agreement on the best approach!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
http://www.coderanch.com/t/364229/Servlets/java/Java-Serializable-Servlets
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!