I'm constructing a new URL in a Spring MVC controller, to be passed back to the client. Currently I'm trying this:
// httpRequest is the current HttpServletRequest
new URL(httpRequest.getProtocol(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
Problem is that httpRequest.getProtocol()
gives me "HTTP/1.1" instead of just "HTTP". I can trim it but wondered if there was a more elegant way.
The protocol is HTTP/1.1, since it is a specific version of HTTP. The scheme as given by ServletRequest#getScheme
itself is http
:
new URL(httpRequest.getScheme(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
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