-
package
com.babyDuncan.java;
-
-
import
java.io.IOException;
-
import
java.io.InputStreamReader;
-
import
java.io.LineNumberReader;
-
import
java.io.PrintWriter;
-
-
import
javax.servlet.ServletException;
-
import
javax.servlet.http.HttpServlet;
-
import
javax.servlet.http.HttpServletRequest;
-
import
javax.servlet.http.HttpServletResponse;
-
-
public
class
ipServlet
extends
HttpServlet {
-
-
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
-
throws
ServletException, IOException {
-
doPost(request, response);
-
}
-
-
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
-
throws
ServletException, IOException {
-
-
String ip = getRemoteAddress(request);
-
System.out.println(ip);
-
System.out.println(getMACAddress(ip));
-
-
response.setContentType(
"text/html"
);
-
PrintWriter out = response.getWriter();
-
out
-
.println(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
);
-
out.println(
"<HTML>"
);
-
out.println(
" <HEAD><TITLE>A Servlet</TITLE></HEAD>"
);
-
out.println(
" <BODY>"
);
-
out.print(
" This is "
);
-
out.print(
this
.getClass());
-
out.println(
", using the POST method"
);
-
out.println(
" </BODY>"
);
-
out.println(
"</HTML>"
);
-
out.flush();
-
out.close();
-
}
-
-
public
String getRemoteAddress(HttpServletRequest request) {
-
String ip = request.getHeader(
"x-forwarded-for"
);
-
if
(ip ==
null
|| ip.length() ==
0
|| ip.equalsIgnoreCase(
"unknown"
)) {
-
ip = request.getHeader(
"Proxy-Client-IP"
);
-
}
-
if
(ip ==
null
|| ip.length() ==
0
|| ip.equalsIgnoreCase(
"unknown"
)) {
-
ip = request.getHeader(
"WL-Proxy-Client-IP"
);
-
}
-
if
(ip ==
null
|| ip.length() ==
0
|| ip.equalsIgnoreCase(
"unknown"
)) {
-
ip = request.getRemoteAddr();
-
}
-
return
ip;
-
}
-
-
public
String getMACAddress(String ip) {
-
String str =
""
;
-
String macAddress =
""
;
-
try
{
-
Process p = Runtime.getRuntime().exec(
"nbtstat -A "
+ ip);
-
InputStreamReader ir =
new
InputStreamReader(p.getInputStream());
-
LineNumberReader input =
new
LineNumberReader(ir);
-
for
(
int
i =
1
; i <
100
; i++) {
-
str = input.readLine();
-
if
(str !=
null
) {
-
if
(str.indexOf(
"MAC Address"
) >
1
) {
-
macAddress = str.substring(
-
str.indexOf(
"MAC Address"
) +
14
, str.length());
-
break
;
-
}
-
}
-
}
-
}
catch
(IOException e) {
-
e.printStackTrace(System.out);
-
}
-
return
macAddress;
-
}
-
}
访问地址:
http://hanvon-babydunc:8080/testServlet/servlet/ipServlet
运行结果:
192.168.10.18
00-26-C7-21-65-46
浏览器会访问DNS服务器将输入的域名对应的IP
获取
到
浏览通过HTTP协议和IP协议处理发送的建立连接的请求然后发送到互联网
在互联网中路由器根据目标
IP地址
,通过复杂的算法找出'最优路径'来传输请求
找到目标IP计算机(服务器)的网卡通过三次握手建立连接
服务器对请求进行分析处理然后返回浏览器需要的页面
浏览器拿到数据会对DOM结构和CSS进行分析形成renderTree然后渲
I was asked to write a
servlet
that collects client's details such as ip, mac address etc.getting his IP is pretty straight-forward (request.getRemoteAddr()) but I dont find an elegant way to get his ...
java
获取
IP地址
和
MAC地址
。 /**
* 通过Http
Servlet
Request返回
IP地址
* @param request Http
Servlet
Request
* @return ip String
* @throws Exception
public String getIpAddr(Http
Servlet
Request request) throws Exception {
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");
public class CheckMACandIP extends Http
Servlet
{ public CheckMACandIP() { super(); } public void destroy() { super.destroy(); } public void doGet(Http
Servlet
Request request, HttpS
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import javax.
servlet
.
Servlet
Except
/* 利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。例子如下(拿cpu个数,其他类似):* 定义命令:* WindowsCmd ="cmd.exe /c echo %NUMB...
java
获取
客户端
IP地址
以及
mac地址
在JSP里,
获取
客户端
的
IP地址
的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能
获取
到
客户端
的真实
IP地址
了。如果使用了反向代理软件,用request.getRemoteAddr()方法
获取
的
IP地址
是:127.0.0.1或192.168.1.110,