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
posted
0
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
posted
0
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
posted
0
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
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
posted
0
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
posted
0
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
posted
0
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.