HttpServletRequest获取浏览器、服务端和客户端信息-LMLPHP

  如何通过HttpServletRequest来获取到上面的属性呢?

1、引入开源工具

<!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>

2、在controller中使用

UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
Browser browser = userAgent.getBrowser();
OperatingSystem operatingSystem = userAgent.getOperatingSystem();
Version browserVersion = userAgent.getBrowserVersion();
log.info(browser.getName());
log.info(operatingSystem.getName());
log.info(browserVersion.getVersion());

3、运行结果

HttpServletRequest获取浏览器、服务端和客户端信息-LMLPHP

4、request的服务端和客户端详细信息

request.getLocalAddr();//获取本地IP,即服务器IP
request.getLocalName();//获取本地名称,即服务器名称
request.getLocalPort();//获取本地端口号,即Tomcat端口号
request.getLocale();//用户的语言环境
request.getContextPath();//context路径
request.getMethod();//GET还是POST
request.getProtocol();//协议,http协议
request.getQueryString();//查询字符串
request.getRemoteAddr();//远程IP,即客户端IP
request.getRemotePort();//远程端口,即客户端端口
request.getRemoteUser();//远程用户
request.getRequestedSessionId();//客户端的Session的ID
request.getRequestURI();//用户请求的URL
request.getScheme();//协议头,例如http
request.getServerName();//服务器名称
request.getServerPort();//服务器端口
request.getServletPath();//Servlet路径
05-08 08:41