Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read serialized objects using HttpServlet

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
0
0
DE
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
 
What error are you getting or what is being the behaviour right now?

Cheers,
Dian
 
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
 
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
 
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
 
You're saying you're getting an internal error. That's an obvious error that should have an exception behind.

Cheers,
Dian
 
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
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top