2012-04-10 141 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