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
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)
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...