Consider the following code sending an HTTP 201 "Created" response to the client:
String url = "/app/things?id=42"; // example response.setStatus(HttpServletResponse.SC_CREATED); response.setContentType("text/plain"); response.setHeader("Location", url); response.getWriter().print(url);
It informs the client that a new "thing" was created and that it can be found at the URL /app/things?id=42
. The problem is that this URL is relative. This would be perfect for a JSP, which might be written as follows:
<img src="<c:url value="/things?id=42" />" />
Which would produce the following HTML:
<img src="/app/things?id=42" />
Which is what we want for web apps.
But I don't believe that is what we want for a 201 response Location header. The HTTP spec states:
14.30 Location
The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource. For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource. The field value consists of a single absolute URI.
Location = "Location" ":" absoluteURI
An example is:
Location: http://www.w3.org/pub/WWW/People.html
My question is how do I translate that relative URL to the abosolute URL for the Location header in the proper way for servlets.
I do NOT believe that using:
request.getServerName() + ":" + request.getServerPort() + url;
Is the correct solution. There should be a standard method that produces the correct output (so that URL rewriting, etc., can be applied). I don't want to create a hack.
The HTTP Location header is a response header that is used under 2 circumstances to ask a browser to redirect a URL (status code 3xx) or provide information about the location of a newly created resource (status code of 201). Its usage is often confused with another HTTP Header which is HTTP Content-Location header.
The Location response header indicates the URL to redirect a page to. It only provides a meaning when served with a 3xx (redirection) or 201 (created) status response.
Response Header: This type of headers contains the location of the source that has been requested by the client. Entity Header: This type of headers contains the information about the body of the resources like MIME type, Content-length.
Setting Response Headers from Servlets The most general way to specify headers is to use the setHeader method of HttpServletResponse. This method takes two strings: the header name and the header value.
Just send the absolute path. The restriction to an absolute URI is a known defect in RFC 2616 and will be fixed in HTTPbis (see http://trac.tools.ietf.org/wg/httpbis/trac/ticket/185).
Please note that RFC 7231 now includes relative URIs in the spec. See other answers for how to handle relative URIs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With