• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java Serializable on Servlets

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

Ive created a Java Package that contains some Serializable classes so that I can save them, and it all runs fine in NetBeans.

I have now created a Servlet inside of the Java package which communicated with some of the classes and placed it onto a TomCat server.

The first time I posted this is linked to someone elses post so ill try again.

I seem to be having some problems with it all though.

Can anyone tell me 100% if I can use Java Serializable classes in this way?

Im not massivly experienced with Servlets, but in the past I have created a Servlets that call classes inside the same package.

I assume this is ok to do and I do not need to make all my classes into servlets?

Thank you very much.

Darren
 
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

Originally posted by Darren Jackson:

I assume this is ok to do and I do not need to make all my classes into servlets?



It's not only OK, it's preferable.
It's a good practice to separate your business logic from any container specific classes (servlets, JSPs, listeners etc). Putting this logic in POJO (Plain old Java objects) means that they can be re-used in non-servlet applications and makes them easier to test from the command line or with automated testing tools like JUnit.
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, I did kind of think that myself.

Looking into my errors a little more closly it seems that my Servlet cannot find the .ser file that it needs.

Ive currently got a usersettings.ser file that the servlet should load when the servlet requested.

This file is sitting in the same folder as all my classes, which is how it was in netbeans.

Do I need to move this file somewhere else or create anything to tell my servlet where this file is?

Im really confused

Thanks

Darren
 
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
How are you setting the path for the .ser file?
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently just like this, inside a java class.



Then passing FILENAME to the InputStream.

Ive not got it definded anywhere else.

usersettings.ser just sits inside the Java package folder on the server.

The error I get is this:

java.io.FileNotFoundException: usersettings.ser (The system cannot find the file
specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at MNO.SaveLoadSettings.loadUserSettings(SaveLoadSettings.java:79)
at MNO.AccurateEstimateServlet.doGet(AccurateEstimateServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
rocessConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
lowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:595)


[ March 19, 2007: Message edited by: Darren Jackson ]
 
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
If all you pass the input stream is the filename, without any path information, Java will try to create the file in the current working directory.
This is fine for command line apps but you can't really count on the location of the current working directory in a server app. It will vary depending on how the server was started (windows service, daemon script, etc...).

Try using an absolute path name for creating your file.
 
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

Originally posted by Ben Souther:

Try using an absolute path name for creating your file.



Spec compliant containers provide a working directory for each application.
The location for this directory is stored in the context scoped "javax.servlet.context.tempdir".

 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, trying to do the absolute path now.

That last post has confused me a little bit.

The directory where ive now put the usersettings.ser file is:

E:\Apache_Tomcat_5.5.20\webapps\mobileapplications\WEB-INF\lib

So what would be my absolute path?

http://localhost:8080/mobileapplications/lib/usersettings.ser ?

String tempdir = (String)getServletContext().getAttribute("javax.servlet.context.tempdir");



Is this just finding the location of the temp folder where I need to store the file?

Thanks

Darren
 
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

Originally posted by Darren Jackson:



So what would be my absolute path?

http://localhost:8080/mobileapplications/lib/usersettings.ser


E:\Apache_Tomcat_5.5.20\webapps\mobileapplications\WEB-INF\lib
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code that I am using:



Do I need to add a line simular to this into this code?



Darren
 
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

Originally posted by Darren Jackson:
Is this just finding the location of the temp folder where I need to store the file?



You don't need to store it there but it is one platform independent way to find a directory that will be unique to that particular web application.
It will also return a real working directory that can be written to whether your app is deployed as an expanded file system or a packed war file.

If your app is running from a packed war file, you won't be able to write to WEB-INF/lib (or anywhere else within the webapp's directory stucture).
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


quote riginally posted by Darren Jackson:
Is this just finding the location of the temp folder where I need to store the file?



You don't need to store it there but it is one platform independent way to find a directory that will be unique to that particular web application.
It will also return a real working directory that can be written to whether your app is deployed as an expanded file system or a packed war file.



So how would I go about saving a file to that location?

Sorry to ask so many questions.

Really not feeling clever tonight :roll:

Cheers

Darren
 
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
Here's some ContextListener code that serializes a Map to disc when a webapp shuts down and then deserializes it when the app starts up.
It should be enough to get you started...


 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that code.

Had a good look at it and its probably more advanced than I need.

All my code does is save one class from the whole package when one of the other classes calls it.

Im convinced that if I can tell the servlet where to find my usersettings.ser file it should work .

Here is the error that Im getting from the server log:


java.io.FileNotFoundException: WEB-INF\lib\usersettings.ser (The system cannot f
ind the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at MNO.SaveLoadSettings.loadUserSettings(SaveLoadSettings.java:81)
at NO.AccurateEstimateServlet.doGet(AccurateEstimateServlet.java:22)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
rocessConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
lowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:595)



Im just going to experiment with different ways of trying to tell the java code where the file is.

Thanks for all you help.

Hope I get it sorted soon

Darren
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to use the below code.

A>
ServletContext sc = getServletContext();
InputStream in = sc.getResourceAsStream("\WEB-INF\lib\usersettings.ser");

But I would prefer to keep the .ser file in WEB-INF folder ,rather than in WB-INF/lib. Lib is ment for jars.

B>
put the file in classes folder so that its visible to the webapp classloader.

InputStream in = <servlerClassName>.class.getClassLoader().getResourceAsStream("Location");

Hope this helps,
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,

I apriciate all the help I get on here.

I just felt this topic was more about Serializable rather than relative paths, so I started a new topic so as not to confuse people.

Anyway.

I understand what people are telling me that I need to create an input stream and pass that to my class that needs the file.

In my class I use:


So if I pass my class an InputStream, I am a little confused how I can then apply it to what I have?

Do I need to replace the FileInputStream with the InputStream I send?

Thank you.

Darren
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive sort it now.

Not quite sure how but it works.

Cheers for the help guys.

 
Sheriff
Posts: 67752
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:
  • Quote
  • Report post to moderator

Originally posted by Darren Jackson:

Not quite sure how but it works.



Not how we like to leave things. What has you bamboozled?
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic