File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forwarding to another servlet

 
Stephane Weber
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephane Weber
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Open a URL connection to the other servlet, get the data as XML .. say , parse it and do your processing....
 
Erick Jones
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic