Cesar
Cesar

Reputation: 1610

Jetty redirect and context path

I have an application running on an embeded jetty server. I have defined the context path:

ServletContextHandler context =...
context.setContextPath("/dev");

I can correctly acces my app http://application.com:8080/dev

When i use the sendRedirect function of the HttpServletResponse like:

resp.sendRedirect("/login");

The URL formed is not using the application context. It is returning http://application.com:8080/login insetad of http://application.com:8080/dev/login

How do i conifgure this path?

Upvotes: 2

Views: 3584

Answers (2)

Stanislav Levental
Stanislav Levental

Reputation: 2225

Try

resp.sendRedirect("login");

Details: If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root. If the location is relative with two leading '/' the container interprets it as a network-path reference (see RFC 3986: Uniform Resource Identifier (URI): Generic Syntax).

Upvotes: 1

David Levesque
David Levesque

Reputation: 22441

When you call sendRedirect() with a location having a leading "/", it is always relative to the server root, not to the application context. To achieve what you want, you have to append the context path yourself, e.g.:

response.sendRedirect(request.getContextPath() + "/login");

For it to work in all contexts, it is better to encode it:

response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/login"));

Upvotes: 6

Related Questions