• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Web App HashMap Serilization

 
Greenhorn
Posts: 14
MySQL Database Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Craig Worsell
Greenhorn
Posts: 14
MySQL Database Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never close the FileInputStream.
 
Craig Worsell
Greenhorn
Posts: 14
MySQL Database Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try closing the FileOutputStream object.
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Craig Worsell wrote:


Don't close out. That's what's causing the latest exception.
 
Craig Worsell
Greenhorn
Posts: 14
MySQL Database Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm sure glad that he's gone. Now I can read this tiny ad in peace!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic