Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the currently used protocol name from HttpServletRequest? [duplicate]

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.

like image 642
user3120173 Avatar asked Dec 06 '22 21:12

user3120173


1 Answers

The protocol is HTTP/1.1, since it is a specific version of HTTP. The scheme as given by ServletRequest#getSchemeitself is http:

new URL(httpRequest.getScheme(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
like image 50
nanofarad Avatar answered Mar 23 '23 00:03

nanofarad