File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Forwarding to another servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forwarding to another servlet" Watch "Forwarding to another servlet" New topic
Author

Forwarding to another servlet

Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
Hi, as you will see I am quite new to servlets, so sorry if the question is not smart
I have this servlet A that handles requests from my clients. These clients ask some data, and A finds out to which other servlet B or C it must request that data.
How can I call servlet B from A, asking it to send back the needed data ?
Thanks,
Stephane
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
B need not be a servlet, it can be an ordinary java component.
You can instanciate it like a normal java class, and invoke methods on it to get back the data you want.
You can also pass the request and response object to it, and it can pass back the data in the request, or it can write it directly to the output stream which it obtains from response.


Shubhrajit
Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
My problem is I have to use a Servlet, but don't see how to forward request and response to it.
The forward method from RequestDispatcher only applies to resources on the same server (not the case here) and sendRedirect seem not to pass the data, as I get a null pointer if I try request.getContentType() in Servlet B, after having set response.setContentType to some value in Servlet A. I presume this is some kind of session problem.
Can anybody help me ?
Thanks
In fact, I have in servlet A :
response.setContentType( "text/xml" );
PrintWriter out = response.getWriter();
out.write("<test></test>");
response.sendRedirect("http://localhost:8080/myApplication/servlet/LocalDemonstratorServlet");
And in servlet B :
String ct = this.request.getContentType();
System.out.println("ct : "+ct);
Which prints null
[ July 02, 2002: Message edited by: Stephane Weber ]
Shubhrajit Chatterjee
Ranch Hand

Joined: Aug 23, 2001
Posts: 356
Open a URL connection to the other servlet, get the data as XML .. say , parse it and do your processing....
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
You cannot forward a request to a servlet in another server. If you use the sendRedirect() method a new request is created. Also, sessions cannot not be shared across servers.

The best solution is probably to configure both servlets in the same server.
Erick
Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
OK thanks Erick, that's what I feared, but now I know for sure
Stephane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forwarding to another servlet
 
Similar Threads
servlet question
Socket Server calling web service
can Http POST Method pass object?
Application level communication
How to share temporary, continuously changing data between clients