首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Servlet怎么得到客户端机器的信息

2012-09-02 
Servlet如何得到客户端机器的信息?Servlet可以使用getRemoteAddr()和getRemoteHost()来得到客户端的IP地址

Servlet如何得到客户端机器的信息?
Servlet可以使用getRemoteAddr()和getRemoteHost()来得到客户端的IP地址和host, 代码如下所示:
用这些方法来访问客户端有所限制,如下代码实现了对客户端配置进行检查并把相关消息发送到客户端的功能:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public?class DemoExportRestriction extends HttpServlet{
public void doGet(HttpServletRequest?req,HttpServletResponse?res)
throws ServletException,IOException{

res.setContentType("text/plain");
PrintWriter?out=?res.getWriter();

//得到客户端的hostname
String?remoteHost?=?req.getRemoteHost();

//查看客户端是否允许这样的操作
if(!isHostAllowed(remoteHost)){
out.println("Access?<BLINK>ACCESS?DENIED?</BLINK>");
} else{
out.println("access?granted");
}
}
private boolean isHostAllowed(String?host)?{
return(host.endsWith(".com"))||
(host.indexOf('.')==-1);//没有域名 ok
}
}

热点排行