File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Web App HashMap Serilization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Web App HashMap Serilization" Watch "Web App HashMap Serilization" New topic
Author

Web App HashMap Serilization

Craig Worsell
Greenhorn

Joined: Sep 22, 2011
Posts: 14

Hi all, I am trying to create a test login jsp/servlet app. For now I want to create a HashMap to store the username and passwords (I would not do this in real life) and then serialize the HashMap.

I would like to open the serliized object to search when the user logs back on and when a new one is created to check the username is not already in use.

I have written a java class for the servlet to instantiate with methods to do both the verification and creation but the serialization part does not seem to work.

My question is this: Am I able to serialize and de-serialize a HashMap in a web app?
This is my current code for doing so:


Many Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Although it's not the best way (but you already know that), it's definitely possible to serialize and de-serialize a HashMap, as long as its contents are also serializable (which Strings are). However, you need to add some synchronization to ensure only one request can access the file at a time, or you risk your data getting inaccurate or even corrupted.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Craig Worsell
Greenhorn

Joined: Sep 22, 2011
Posts: 14

you need to add some synchronization to ensure only one request can access the file at a time


I thought I'd get the actual serilization to work before adding this, but thank you.

Should the code I posted work for my intended purpose from what you can see?
Below is what should call it which is in my servlet:

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

You never close the FileInputStream.
Craig Worsell
Greenhorn

Joined: Sep 22, 2011
Posts: 14

Thanks Rob. I have fixed that but it is still not serializing the object it I am getting the below messages:

WARNING: Internal error flushing the buffer in release()
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.io.IOException: Stream closed

Although I am not closing the steam...
Any thoughts?

Thanks again.
Ragavendran Baskaran
Greenhorn

Joined: Dec 12, 2011
Posts: 15
Try closing the FileOutputStream object.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Craig Worsell wrote:

Don't close out. That's what's causing the latest exception.
Craig Worsell
Greenhorn

Joined: Sep 22, 2011
Posts: 14

Thanks both. I also worked out that I needed to put the full path to save the .ser file, and as I am running off the localhost I had to put the path for my local drive (C:\) where my project is saved. However if this was a live web app how would I save the .ser file to the server as I tried http://localhost:8080/WebApplication1/... but this did not work?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Web App HashMap Serilization