• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing object from one servlet to the other.

 
Pravin S
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please let me know how to pass a complete object from one servlet to the other servlet.
 
Peter Kristensson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gerard Nik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic