WICKET: how to get client's ip/address
I'm using wicket 1.5开发者_JAVA技巧.1, couldn't figure this out.
public class MyPage extends WebPage {
public MyPage() {
String clientAddress = ...?
WebRequest req = (WebRequest) RequestCycle.get().getRequest();
HttpServletRequest httpReq = (HttpServletRequest) req.getContainerRequest();
String clientAddress = httpReq.getRemoteHost();
Subclass WebClientInfo
to provide a public method that delegates on protected WebClientInfo.getRemoteAddr()
. Then create a method to query this in a custom RequestCycle
class. In Wicket 1.3-1.4 I've achieved this by subclassing RequestCycle
, but with 1.5 it seems things are different: RequestCycle in Wicket 1.5
WebClientInfo
has the advantage of querying the X-Forwarded-For
erquest parameter, and will return the proper IP address if your server is behind a proxy/load balancer that uses XFF.
Using Wicket 6 and 7, you can do the following:
String remoteAddress = ((WebClientInfo)Session.get().getClientInfo())
.getProperties()
.getRemoteAddress();
精彩评论