如何获取正在访问网站网址的用户的IP?

时间:2012-03-03 19:49:30

标签: jsp

我一直在尝试使用以下代码获取用户的ip,我得到了这个输出:

  

InetAddress.getLocalHost()。getHostName():ankur-PC

     

InetAddress.getLocalHost()。getHostAddress():10.0.0.2

     

request.getLocalAddr():0.0.0.0

     

request.getLocalName():0.0.0.0

     

request.getRemoteAddr():0:0:0:0:0:0:0:1

     

request.getRemoteHost():0:0:0:0:0:0:0:1

     

request.getHeader(“Proxy-Client-IP”):null

     

request.getHeader(“WL-Proxy-Client-IP”):null

     

request.getHeader(“X-FORWARDED-FOR”):null

     

InetAddress.getByName(request.getHeader( “X-转发-FOR”)):   localhost / 127.0.0.1

     

InetAddress.getByName(request.getRemoteAddr()):/ 0:0:0:0:0:0:0:1

     

InetAddress.getByName(request.getRemoteHost()):/ 0:0:0:0:0:0:0:1

我不知道它的正确与否。请给我一些建议,因为我是新手。

    /*
 * getClientIP.java
 *
 * Created on July 9, 2011, 12:13 AM
 */

//package org.com.testing;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;



public class getClient extends HttpServlet {

   // Translation translation = new Translation();

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();

        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<html>");
        out.println("<!-- Copy right 2009-2012 Redecis Systems Inc. All Rights Reserved. -->");
        out.println("<head>");
        out.println("<title>::: Redecis Systems :::</title>");
        out.println("<meta http-equiv=\"pragma\" content=\"no-cache\">");
        out.println("<meta http-equiv=\"expires\" content=\"0\">");
        out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
        out.println("<meta name=\"Author\" CONTENT=\"Kevin\">");
        out.println("<link rel=\"stylesheet\" href=\"JS/Standar.css\" type=\"text/css\">");
        out.println("<script type=\"text/javascript\" src=\"JS/Standar.js\" language=\"JavaScript\"></script>");
        out.println("</head>");
        out.println("<body>");

        out.println("<table width=\"100%\" height=\"100%\" cellpadding=\"0\" cellspacing=\"0\">");
        out.println("<tr>");
        out.println("<td align=\"center\" valign=\"middle\">");

        out.println("<table class=\"tableBorder\" width=\"50%\" height=\"35%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"> ");
        out.println("<tr>");
        out.println("<td>");

        out.println("<table class=\"darkBlue\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"0\"> ");

        out.println("<tr>");
        out.println("<td align=\"right\">InetAddress.getLocalHost().getHostName():</td>");
        out.println("<td align=\"left\">" + InetAddress.getLocalHost().getHostName() + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">InetAddress.getLocalHost().getHostAddress():</td>");
        out.println("<td align=\"left\">" + InetAddress.getLocalHost().getHostAddress() + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td width=\"65%\" align=\"right\">request.getLocalAddr():</td>");
        out.println("<td width=\"35%\" align=\"left\">" + request.getLocalAddr() + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">request.getLocalName():</td>");
        out.println("<td align=\"left\">" + request.getLocalName() + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td width=\"65%\" align=\"right\">request.getRemoteAddr():</td>");
        out.println("<td width=\"35%\" align=\"left\">" + request.getRemoteAddr() + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">request.getRemoteHost():</td>");
        out.println("<td align=\"left\">" + request.getRemoteHost() + "</td>");
        out.println("</tr>");


        out.println("<tr>");
        out.println("<td align=\"right\">request.getHeader(\"Proxy-Client-IP\"):</td>");
        out.println("<td align=\"left\">" + request.getHeader("Proxy-Client-IP") + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">request.getHeader(\"WL-Proxy-Client-IP\"):</td>");
        out.println("<td align=\"left\">" + request.getHeader("WL-Proxy-Client-IP") + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">request.getHeader(\"X-FORWARDED-FOR\"):</td>");
        out.println("<td align=\"left\">" + request.getHeader("X-FORWARDED-FOR") + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">InetAddress.getByName(request.getHeader(\"X-FORWARDED-FOR\")):</td>");
        out.println("<td align=\"left\">" + InetAddress.getByName(request.getHeader("X-FORWARDED-FOR")) + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">InetAddress.getByName(request.getRemoteAddr()):</td>");
        out.println("<td align=\"left\">" + InetAddress.getByName(request.getRemoteAddr()) + "</td>");
        out.println("</tr>");

        out.println("<tr>");
        out.println("<td align=\"right\">InetAddress.getByName(request.getRemoteHost()):</td>");
        out.println("<td align=\"left\">" + InetAddress.getByName(request.getRemoteHost()) + "</td>");
        out.println("</tr>");

        out.println("</table>");

        out.println("</td>");
        out.println("</tr>");
        out.println("</table>");

        out.println("</td>");
        out.println("</tr>");
        out.println("</table>");

        out.println("</head>");
        out.println("</body>");
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

1 个答案:

答案 0 :(得分:4)

是。request.getRemoteAddr()返回客户端IP 唯一的问题是当用户在代理后面时。使用它将返回代理服务器的地址。在这种情况下,要检索客户端的ip,我们使用request.getHeader("x-forwarded-for")