2012-04-10 140 views
2

我有一个非常基本的XMLRPC Servlet服务器运行 - 字面上遵循由Apache人(http://ws.apache.org/xmlrpc/server.html)建议的默认值。在Apache XMLRPC环境中获取请求用户的IP地址?

有没有什么办法让我从我的XMLRPC函数中访问请求者的IP地址?我正在设计一项服务,记录通过IP地址从不同用户接收的请求。

例如,如果我服用从他们的榜样Calculator类,我可能会做这样的事情,

public int add(int a, int b){ 
    IPAddress user = {magic incantation}; 
    Log.info("Summed " + a + " and " + b + " for " + user); 
    return a + b; 
} 

(显然这是一个玩具的例子,但如果我知道该怎么做,我可以做我想做的事我的程序)

非常感谢!

回答

1

处理请求时,您可以访问HttpServletRequest的实例。该对象提供了方法getRemoteAddr()

ALSO:在常见问题解答中,您会发现this snippet获取和存储IP为ThreadLocal,因此您可以在此后访问它(也许这比您想要的要多)。

片断的再现是:

public static class ClientInfoServlet extends XmlRpcServlet { 
    private static ThreadLocal clientIpAddress = new ThreadLocal(); 

    public static String getClientIpAddress() { 
     return (String) clientIpAddress.get(); 
    } 

    public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse) 
      throws IOException, ServletException { 
     clientIpAddress.set(pRequest.getRemoteAddr()); 
     super.doPost(pRequest, pResponse); 
    } 
} 
+0

工作太棒了!非常感谢你! – justinemarie 2012-04-11 05:58:21