Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Java Serializable on Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java Serializable on Servlets" Watch "Java Serializable on Servlets" New topic
Author

Java Serializable on Servlets

Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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

Joined: Dec 11, 2004
Posts: 13410

How are you setting the path for the .ser file?
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 18, 2007
Posts: 42
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 18, 2007
Posts: 42
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 18, 2007
Posts: 42

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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 18, 2007
Posts: 42
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
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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,


Rahul Bhattacharjee
LinkedIn - Blog
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
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

Joined: Jan 18, 2007
Posts: 42
Ive sort it now.

Not quite sure how but it works.

Cheers for the help guys.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Originally posted by Darren Jackson:

Not quite sure how but it works.


Not how we like to leave things. What has you bamboozled?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
wood burning stoves
 
subject: Java Serializable on Servlets
 
Similar Threads
Doubt in servlet
How i can connect my servlet to mysql database
Still serialization problem with DTO
accessing .class file from jsp
I'm having problems putting my own classes into packages