×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to read serialized objects using HttpServlet

How to read serialized objects using HttpServlet

How to read serialized objects using HttpServlet

(OP)
Hi,

I have a servlet code that saves data input from a form in HTML into a serial file. For example, I've input as following:
First Name: John
Last Name: Doe
Address: First Avenue 11111 New York

Then, I want to read this input again, by giving in only the first name or the last name. So, if I input "Doe", I will be able to see his first name and his address. This is my problem.

I know it may not be recommmended, but I'm in such a desperate mode that I want to give you the complete code (but I don't think it's long). Here is the code that saves data (write) and runs perfectly for that purpose, but I want to do the exact opposite (read):

CODE


// here bunch of import commands...

public class AdressenEingabe extends HttpServlet {

    String anfrage;
    String adressFileName = "./htdocs/JavaServer/Adressen.ser";
    PrintWriter out;
    Vector adressen;
    DateiAusgabe logger;
    SerHandler serializer;
    HtmlWrapper wrapper;
    Adresse adresse;
    
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        logger = new DateiAusgabe("./logs/AdressenServlet.log");
        logger.setDatumFormat(DateFormater.getTimeFormat(new Date(), null));
        logger.setLogLevel(AusgabeFormat.DEBUG);
        serializer = new SerHandler(logger);
        adressen = (Vector)serializer.load(adressFileName);
        if (adressen == null) adressen = new Vector();
    }
    
    public void destroy() {
        serializer.save(adressFileName, adressen);
    }
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        res.setContentType("text/html");
        out = res.getWriter();
        wrapper = new HtmlWrapper(out, getServletConfig());
        if (req.getQueryString() != null) {
            adresse = new Adresse(req);
            adressen.add(adresse);
            serializer.save(adressFileName, adressen);  
        }
        wrapper.wrapMessage("Result", adressen.toString());
    }
    
    public void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
        doGet(req, res);
    }
    
}
The serial file is Adressen.ser, and my serial class is serHandler, and it has load method (with readObject) and save method (with writeObject).

I'm totally lost, and would be very appreciated for any clue!

Thanks,
Andre

RE: How to read serialized objects using HttpServlet

What error are you getting or what is being the behaviour right now?

Cheers,
Dian

RE: How to read serialized objects using HttpServlet



To read a serialized object from a file :

CODE

FileInputStream fis = new FileInputStream (fileName);
ObjectInputStream ois = new ObjectInputStream (fis);
Object object = ois.readObject();
fis.close();

// Now case from object to your specific type

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
http://www.primrose.org.uk

RE: How to read serialized objects using HttpServlet

(OP)
Dian, I have tried to play with the codes in that AdressenEingabe servlet, all I got is either internal error from the server, or the class still does the write function, not read.

Sedj,
the code that you gave me is already in my serHandler class:

CODE

...
public synchronized Object load(String fileName) {
    File file = new File(fileName);
    if(file.exists()) {
      try {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
        ObjectInputStream in = new ObjectInputStream(bis);
        Object returnObj = in.readObject();
        in.close();
        return returnObj;
      }
            // catch here
    return null;
}
...
I think I just have to modify the AdressenEingabe servlet I gave on my first post, so that it does exactly the opposite (that it reads the serialized object). I just don't know what to look up.

More clues please... :))

Andre

RE: How to read serialized objects using HttpServlet

What error do you get?

Cheers,
Dian

RE: How to read serialized objects using HttpServlet

(OP)
Like I said, I don't get any obvious error. I have played around with the code in the AddressenEingabe class. I tried to, for example, change the save method with load method. At one point, I received an internal error from the browser, like it cannot recognize the code, etc. At the other point, the servlet still does the save function instead of load, which I want it.

The real problem is, I don't really know where to start, or which class I should be focusing on. But at least these two classes (AdressenEingabe and serHandler) play a big roll.

Do you have a reference about read a serialized object with HttpServlet that I can learn from?

Andre

RE: How to read serialized objects using HttpServlet

You're saying you're getting an internal error. That's an obvious error that should have an exception behind.

Cheers,
Dian

RE: How to read serialized objects using HttpServlet

I really do not see the problem here - if you want to load a serialized object, then load it. If you want to save an object to disk, then do that.

Please try and explain a little more clearly what you are trying to do !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
http://www.primrose.org.uk

RE: How to read serialized objects using HttpServlet

(OP)
Hi,

What I want is to read the serialized object that have been saved in a serial file before (the file is called "addressen.ser" here).

I already have a class called "serHandler" that handles the writeObject and readObject method.

In the code from "AddressenEingabe" class (from my first post) in doGet method, you will see:

CODE

public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
...
serializer.save(adressFileName, adressen);  
...
}
in which serializer is an instance of serHandler class, and save is a method in serHandler that handles writeObject. I changed this "save" method into "load" method, which handles readObject in serHandler class, expecting that it will read the objects, so it looks like this:
serializer.load(adressFileName);

But for some reason the program still does the write function (it saves the input into the serial file, not read it. In my case, when I put "Doe", instead of giving me "John" and his address, the program saves this "Doe" into the file, so I have two "Doe"s). This I don't understand.

Well, I hope it's clearer now.  

Cheers,
Andre

RE: How to read serialized objects using HttpServlet

So you say the load method still writes the Object. I can think on two options:

1.- The load method is not well written, you'd need to look at the source code.

2.- You're not doing what you think you're doing. Try adding some tracin info so you make sure you're calling the rigth method.

Cheers,
Dian

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close