如何在jsp页面中显示客户端ip?

时间:2015-11-30 10:26:42

标签: java linux jsp

我正在尝试获取查看我的jsp页面的系统的ip。当客户端系统是windows时,它工作正常。但是在Linux中它不工作。我使用以下代码来执行此操作。可以任何人建议我解决我的问题

代码:

Mac.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="mc" method="post">
<input type="submit" value="Send">
 <p>Your IP address is: ${pageContext.request.remoteAddr}</p> 
<% String ipAddress = request.getHeader("X-FORWARDED-FOR");
String is="";
if (ipAddress == null) {
    is = request.getRemoteAddr();
}
String OS="";
String userAgent = request.getHeader("User-Agent");
if((userAgent.toLowerCase().indexOf("windows") >= 0)){
    OS="windows";
}
else if((userAgent.toLowerCase().indexOf("linux") >= 0)){
    OS="linux";
}

%>


<p>address:<%=is%>  <%=userAgent%></p>
<input type="text" value="<%=is%>" name="ip">
<input type="text" value="<%=OS%>" name="os">
</form>

</body>
</html>

findMac.java

package checkingfiles;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class mc
 */
@WebServlet("/mc")
public class mc extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public mc() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out=response.getWriter();

        Pattern macpt = null;
        String c="";

         String ip=request.getParameter("ip");
         String OS=request.getParameter("os");
            System.out.println(ip);

            System.out.println(OS);
            String[] cmd={};
            if (OS.contains("win")) {
                // Windows
                macpt = Pattern
                        .compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
                String[] a = { "arp", "-a", ip };

                cmd = a;
            } else {
                // Mac OS X, Linux

                macpt = Pattern.compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");

              //String ping="ping"+ip;
                String ping="ping 192.168.1.136";

              try {

                  Process p = Runtime.getRuntime().exec(ping);
                  BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
                  String s = "";
                  inputStream.close();
                  p.destroy();

                  } catch (Exception e) {
                  e.printStackTrace();
                  }

                 c="ifconfig -a";
               System.out.println(ip);
            }

            try {
                // Run command
                Process p=null;
                if(OS.contains("win")){
                 p = Runtime.getRuntime().exec(cmd);
                }
                else{
                    p = Runtime.getRuntime().exec(c);
                }
                p.waitFor();

                // read output with BufferedReader
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line = reader.readLine();
                System.out.println("command"+line);
                // Loop trough lines
                while (line != null) {
                    Matcher m = macpt.matcher(line);

                    // when Matcher finds a Line then return it as result
                    if (m.find()) {
                        System.out.println("Found");
                        System.out.println("MAC: " + m.group(0));
                       // return m.group(0);
                    }

                    line = reader.readLine();

                }

            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

    }

}

输出 您的IP地址是:127.0.0.1

地址:127.0.0.1 Mozilla / 5.0(X11; Ubuntu; Linux i686; rv:36.0)Gecko / 20100101 Firefox / 36.0

如何在jsp页面中显示linux系统ip

2 个答案:

答案 0 :(得分:1)

根据评论中的讨论,服务器和客户端都在同一个系统中

localhost的IP地址是127.0.0.1

所以你得到的输出是127.0.0.1

答案 1 :(得分:0)

从各种标题中,您可以获取客户端的IP地址。尝试检查以上所有标题。

    public static String getClientIpAddr(HttpServletRequest request) {
            String ip = request.getHeader("X-Forwarded-For");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("Proxy-Client-IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("WL-Proxy-Client-IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("HTTP_CLIENT_IP");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
                ip = request.getRemoteAddr();

            return ip;
        }