Java – httpservletrequest#getheader (“user agent”) returns null browser name

I'm using java 6 I know very little about JSP and servlet

I'm using the following code to get the name of the browser running my application:

String browserName = requestProvider.get().getHeader("User-Agent");

In addition, I use the following code to get the IP address of the computer running my application:

String ipAdd = requestProvider.get().getRemoteAddr();

In both cases, requestprovider is a reference variable of provider < HttpServletRequest > And I'm sure it will never be null

The problem now is that sometimes I get two values (browsername and ipadd) as null I write sometimes because I don't have test cases

So my question is, what happens in Java when these values can be null?

What should we pay attention to when coding to avoid this problem?

Is there any other way to get IP address & browser name?

Solution

Null indicates that the person sending the request does not include the "user agent" header

It is unlikely to return null under normal circumstances, but it is reported that it may do so under edge conditions, such as after the response has already been sent In any case, "get the IP address of the machine running my application" doesn't sound like getremoteaddr() It is used to get the address of the latest client or agent sending the request

no You rely entirely on the HTTP client and / or any intervening proxy behavior to obtain such information

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>