Andrei Botalov Andrei Botalov - 3 months ago 11
Java Question

Servlets: sendRedirect is redirecting to incorrect url?

I run my app from Eclipse's Servers view. Currently urls that are shown to the user look like:

http://localhost:8082/EclipseProjectName/path


I haven't specified this EclipseProjectName anywhere in my files but it's present there.

Currently when I invoke
response.sendRedirect("/path")
, then user is redirected not to
http://localhost:8082/EclipseProjectName/path
, but to
http://localhost:8082/path


How do I solve that problem?

Answer

You should redirect to response.sendRedirect(request.getContextPath() + "/path"); But I don't know whether it's a correct way to do it