Is there a reason that ServletRequest.getRemoteAddr() returns the clients ip address as a string instead of as an int/long/IPSomethingInstance?
Or asked an other way: Is there any case where the value returned by getRemoteAddr() can't be parsed as a numeric ip addres开发者_开发知识库s?
Returning an ip address as a string, and then having the developer translate it to an ipV4/IpV6 address seems like a wrong design to me, unless there really is cases where the method returns something other then a string.
This method is there as a direct equivalent of the CGI variable REMOTE_ADDR
, so I would guess that's the reason why it's string. And for what's the reason the CGI variable to be string - I don't know.
Presumably to be consistent with getRemoteHost()
, which can return an IP or a resolved name.
If a container returns something other than an IP from ServletRequest.getRemoteAddr(), then it's not complying with the API:
"Returns the Internet Protocol (IP) address of the client
or last proxy that sent the request."
I can only guess that it's done that way because the java.net classes can be a little odd.
I do agree it would be convenient to return the address as an object which provides helpful methods for dealing with IP Addresses.
String works just fine though and is more flexible to changes in IP Address formats in the future (though that's speculative design, which probably isn't a great practice).
I would say, the IP can impossibly be numeric since the single parts of the IP are separated by dots and colons (IPv6). I suppose you're talking about Java. Unfortunately, I don't know much about Java, so I don't know if there is any IPSomethingInstance
. But if it bothers you that much, why don't just write an own?
Also I assume that it's easier to pass over a string to functions and methods taking an IP as a parameter than an instance of a rather unnecessary native object.
精彩评论