passing objects between servlets

Hi,

I am designing a java applciation which has to pass a object to another java applciation. I am planing to use a servlet API communicate with the Servlet in the other application.

In this situation can I pass any serilizable object(ex. Hashtable) between the servlets. Please note that two servlets are in different location.

I will be using java.net.URL and java.net.Connection objects to establish the connection with the servlets.

Thanks for your time and suggestions

thanks

Meena

[530 byte] By [venkataramanana] at [2007-10-3 1:35:47]
# 1

Hellow, Meena

I have one suggestion: You use the method called forward(request, response) to establish communication between two servlets, this one is on class RequestDispatcher in the package javax.servlet.*;

You send objects to another servlet by javax.servlet.http.HttpServletRequest, take a look a sample bellow:

public class servlet1 extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

try{

RequestDispatcher rd = req.getRequestDispatcher("http://server/APP/servlet2");

req.setAttribute("name1", "object1");

req.setAttribute("name2", "object2");

rd.forward(req, resp);

} catch(Exception e){

e.printStackTrace();

}

}

Thanks

Alexandre (Brazil)

AlexandreACPa at 2007-7-14 18:33:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

If your object is Serializable, you can use a URLConnection to connect Servlet 1 to Servlet 2. Then wrap the OutputStream (URLConnection.getOutputStream) with an ObjectOutputStream...

In Servlet 2, wrap the InputStream (ServletRequest.getInputStream) with an ObjectInputStream and read your object.

Obviously both applications will have to have access to the Class...

linxpdaa at 2007-7-14 18:33:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
AlexandreACP...I don't believe your method will work. The path to obtain a RequestDispatcher must point to a resource within the ServletContext.This particular issue is trying to pass an Object across different Contexts.
linxpdaa at 2007-7-14 18:33:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...