• Advertisement


This topic is now archived and is closed to further replies.

Servlet - return an object instead of text

This topic is 5182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive been working with java servlets and so far, all Ive done is pass text (HTML) back to the web page or applet that called it. However, I need to pass an Object back instead. Is this possible? If so, could someone please give some sample code, or at least point me in the right direction. Here is the servlet code that I currently use to send text back from the servlet:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		PrintWriter p=response.getWriter();
		String name = request.getParameter("name");
		String rs="Hello, " = name;

and here is the code on the client (applet) that gets this text data:

private String getFromServlet(String servletAddress, String query) {
		try {
			// Construct a URL referring to the servlet :

			URL url = new URL(servletAddress+"?"+query);
			// form a connection with the servlet :

			URLConnection con = url.openConnection();

			// Prepare for output


			// Turn off caching


			InputStream in = con.getInputStream();
			BufferedReader result = new BufferedReader(new InputStreamReader(in));
			StringBuffer buf = new StringBuffer();
			String s = "";

			while ((s = result.readLine()) != null) {
				buf = buf.append(s).append("\n");

			return buf.toString();

		} catch (Exception e) {
			System.out.println("exception in getFromServlet: "+e);
			return "";

As I said before, this only allows you to pass strings from the servlet to the client, and I dont see how you could pass an object instead, since from the code, it seems like text is the only thing you can possibly pass. I know there has got to be a way to pass objects instead. Im just looking for the wrong thing i guess. Please help! [edited by - AndreTheGiant on December 15, 2003 10:31:26 AM]

Share this post

Link to post
Share on other sites

Thanks for your reply, but I think I need more info. Do you mean serialize it into a string somehow, so that all you need to do is pass a string, and then rebuild the object on the other side?

Hmm if there is anyway to pass an object instead, I would still much rather do that. Its much more direct.

Share this post

Link to post
Share on other sites
Never done any java programming, but by the looks of your code, all the client does is open an http request to the server, the server then calls the java servlet applet which produces the text that the server replies with. to send an object you would need to send some form of binary data with the object in it, and have a way to execute that object from the client. Now even if you could send java bytecode, i very much doubt there is a way of getting the java VM to interpret it as bytecode.

I think SecondBests suggestion is probably the only solution, you basically convert the entire contents of the object into the string, you send that string, and then you rebuild the object given the data from the object on the other side.

I'm not sure exactly what you mean by an object so i'm assuming you mean the equivelent of a C++ class though..

[edited by - Narcist on December 15, 2003 11:43:32 AM]

Share this post

Link to post
Share on other sites
Yea basically the equivalent of a c++ class.

All the example code I could find is similar to what I posted: basically opening up a stream, for sending character data.

You can easily turn this character data back into a java String on the other end, but as for objects other than Strings, I dont know of any way to do it. For example, I would like to return a Vector or a QueryResult (user defined) object.

I could just send a string representation of the object, and reconstruct it on the other end, but that seems like an extremely bad design for what I want to do, and if there is any other way to send the data other than character streams, I would rather use that.

Thanks all.

Share this post

Link to post
Share on other sites

  • Advertisement