0

I have a servlet engine as frontend web server and want forward some request to a backend server. A redirect is not possible.

Are there any helpful API in the servlet spec? Or are the some samples for such feature?

I know I can do it via HttpUrlConnection. But are there some simpler solutions?

Horcrux7
  • 23,758
  • 21
  • 98
  • 156
  • You can do a redirect using `response.sendRedirect([URL])` provided that server is accessible to the client. – Ravindra Gullapalli Mar 26 '13 at 11:54
  • You have the two possible solutions in your question. – Romain Hippeau Mar 26 '13 at 11:55
  • You can forward the request if the "backend server" is running in the same web container, that means it runs just in another servlet context. Is that the case in your environment? – Uooo Mar 26 '13 at 12:43
  • @RavindraGullapalli I have write that a redirect is not possible. The cause is that the browser has no access to the backend server. The backend server is blocked from a firewall. – Horcrux7 Mar 26 '13 at 13:22
  • @w4rumy No the backend server run on another server and is not a servlet container. – Horcrux7 Mar 26 '13 at 13:25

2 Answers2

2

You cannot forward a request to another server. Forwarding works if the servlet you are forwarding to is in the same server.

So your alternatives are:

  • a redirect to the backend,
  • sending a HTTP request to the backend, extracting the results and passing back to the original client in as the respond to the original request, or
  • something else that doesn't involve HTTP at all.

Are there any helpful API in the servlet spec?

None apart from the ones that you have already found (obviously).

But are there some simpler solutions?

AFAIK, no. In particular a "forward" is no an option if the backend is a different server.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

I found the ProxyServlet from Jetty now. This seems a good sample to start.

Horcrux7
  • 23,758
  • 21
  • 98
  • 156