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: