This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Servlets and the fly likes passing  object from one servlet to the other. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing  object from one servlet to the other." Watch "passing  object from one servlet to the other." New topic
Author

passing object from one servlet to the other.

Pravin S
Greenhorn

Joined: Aug 12, 2002
Posts: 5
Can anybody please let me know how to pass a complete object from one servlet to the other servlet.
Peter Kristensson
Ranch Hand

Joined: Jul 02, 2001
Posts: 118
If you're forwarding your request from servlet A to servlet B, you can do something like this:
servlet A:

and in servlet B:

of course you need to cast you object in servlet B
Pravin S
Greenhorn

Joined: Aug 12, 2002
Posts: 5
thanks Peter,
But I think because I just need to pass an object from one servlet to the other, I shouldn't put it as an attribute of the session. I can use the
getAttribute and setAttribute methods of the request object.
Pravin
Premkumar N
Greenhorn

Joined: Apr 21, 2001
Posts: 22
There is one way ...
U can create a singleton class that stores the object to be shared in a hashtable..

..session-id being the key for the hashtable
Eg.. MySharedObject
Hashtable myHash
At Clinet Servlet:
MySharedObject.getMyObject("12312312") // hash key
or
MySharedObject.putMyObject("12312312", obj)
// getMyObject would return the object that is stored in the hash corresp to the current session-id.

Btw why do u want to do such a thing in the first place.. ?? it seems u are heading for a design-flaw !!
Thanks,
Premkumar


<a href="mailto:pondyprem@yahoo.com" rel="nofollow">pondyprem@yahoo.com</a> <br />Sr.Programmer Analyst ( Bioinformatics)<br />Monsanto Entrprises
Gerard Nik
Greenhorn

Joined: Aug 10, 2002
Posts: 9
I think the best way would be to set the object as a request attribute and forwarding the request to the destination servlet where the object can be retrieved.
In the sender servlet
request.setAttribute("name",object);
getServletConfig().getServletContext).getRequestDispatcher(URL).forward(request,response);
In the destination servlet
object = (Classtype)request.getAttribute("name");
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Welcome to the JavaRanch! Please adjust your display name to meet
the JavaRanch Naming Policy. You can change it here.
Thanks!
and welcome to the JavaRanch!
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
There is another more complex means... you can use serialization. I used it once for a project. The client (a servlet in this case) serializes the object to be sent and then converts its bytes to a String of byte values and sends that in the request...
the server (another servlet) receives the request, converts the string into a byte stream and deserializes the object.
It worked really well, and was not as slow as it may sound.
Hope this helps.


- Chris Stehno, SCPJ
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Premkumar N:
There is one way ...
U can create a singleton class that stores the object to be shared in a hashtable..

..session-id being the key for the hashtable
Eg.. MySharedObject
Hashtable myHash
At Clinet Servlet:
MySharedObject.getMyObject("12312312") // hash key
or
MySharedObject.putMyObject("12312312", obj)
// getMyObject would return the object that is stored in the hash corresp to the current session-id.

Btw why do u want to do such a thing in the first place.. ?? it seems u are heading for a design-flaw !!
Thanks,
Premkumar

There are built-in mechanisms for sharing objects in the servlet API (request, session, context). You don't need to create this "singleton" class (not really an example of the singleton design pattern). This would be just like having a global variable! Yuck!


James Carman, President<br />Carman Consulting, Inc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing object from one servlet to the other.
 
Similar Threads
Servlet and Socket
Servlet Context without servlet
How to save the servlet response to a file !
what is the purpose of init() method of Servlet ?
Do we return to the redirecting servlet after response.sendRedirect