0

client calling one rest service and its response redirecting to another rest service using sendRedirect() method

response.sendRedirect("http://localhost:8080/com.youtube.rest/api/v1/status/toreply"); .

from the second service result back to client.but when redirecting response from service 1 to service 2 , service 1 acknowledge back to client with its status 302. i don't want to return back any acknowledgement from service 1 to client. how can i avoid that acknowledgement ? or i want to return back status 200 acknowledgement .please suggest some way to resolve this.

nichu09
  • 804
  • 2
  • 15
  • 40

1 Answers1

2

Sending back status code 302 is exactly what you told the server to do! That's what redirect does: It tells the client that it should lookup the resource under a different URL!

The status code for redirect is 302 (Found). Since HTTP 1.1 303 (See other) or 307 (Moved temporarily) can be used too, but the servlet specification explicitely says that 302 must be used. See also JavaDoc of HttpServletResponse#sendRedirect:

(...) Calling this method sets the status code to SC_FOUND 302 (Found). (...)

Forward to a resource in the same web-application

If you instead want to forward the request to another resource/servlet in the same web application, use a RequestDispatcher:

RequestDispatcher dispatcher =
    request.getRequestDispatcher("path-to-local-content");
dispatcher.forward(request, response);

Forward to a resource in another web-application on the same server

If the content is on the same server but in another web application, the RequestDispatcher must be created via ServletContext:

ServletContext context = request.getServletContext();
RequestDispatcher dispatcher =
    context.getRequestDispatcher("/<other-webapp>/<path-to-content>");
dispatcher.forward(request, response);

Note that some server do not allow cross-context dispatching and return a null dispatcher. On Tomcat, cross-context dispatching is disabled by default but can be enabled in the Context configuration.

Include content from 'foreign' web-servers

If the content is not hosted locally on the same server (more precise: by the same servlet engine), you must open an HttpURLConnection to the foreign web server and retrieve the data by yourself.

See also:

Community
  • 1
  • 1
isnot2bad
  • 22,602
  • 2
  • 27
  • 43
  • Hi..i agreed with you.if i want to use RequestDispatcher two service should be in same server.its working fine in servlet.but when i used in rest service its giving error.in my scenario client will call only one time. client shouldn't know what happening next and it will wait for only result.so how can i achieve this scenario ? – nichu09 Nov 25 '13 at 12:20
  • Is your second service "in the same server"? If yes, is it also within the same web application? What error do you get? – isnot2bad Nov 25 '13 at 12:25
  • i'm using rest service hosted in tomcat.its not going to service2.its giving null error in RequestDispatcher object. – nichu09 Nov 25 '13 at 12:43
  • @nixen09 i've extended my answer to address cross-context-dispatching. – isnot2bad Nov 25 '13 at 13:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41875/discussion-between-nixen09-and-isnot2bad) – nichu09 Nov 25 '13 at 13:21