在基于Servlet的Web应用程序中获取LAN客户端计算机名称 [英] Get LAN client machine name in servlet based web application
问题描述
我有在局域网中运行的spring MVC应用程序.在那儿,客户端计算机的IP地址不时改变.因此,我想获取客户端计算机名称(它们的计算机名称是固定的),因为我想在不创建登录名的情况下获取客户端计算机的详细信息.
I have spring MVC application, that runs in LAN. In there client machines IP addresses are changing time to time. Therefore I want to get client machines names(Their machine name is fixed ),because I want to get client machine's details without creating log in.
有可能获得客户端计算机的名称吗?如果有可能怎么办? 还是有其他方法可以获取该用户的详细信息
Is that possible to get client machine's name?? if it's possible how?? Or is there any other way to get that user details
到目前为止我尝试过的代码
codes I have tried so far
在HttpServlet中
In HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String hostname = request.getRemoteUser(); //this gives null
String hostname = request.getRemoteHost(); //This gives host machine name
}
回复@Eugeny Loy 在web.xml
reply to @Eugeny Loy In web.xml
<init-param>
<param-name>jcifs.smb.client.username</param-name>
<param-value>username</param-value>
</init-param>
在serverlet类中
In serverlet class
String username = config.getInitParameter("username");//This gives client IP address
推荐答案
我找到了获取客户端计算机名称的方法.
I found the way to get client machine's name.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
Logger.getLogger(this.getClass()).warning("Inside Confirm Servlet");
response.setContentType("text/html");
String hostname = request.getRemoteHost(); // hostname
System.out.println("hostname"+hostname);
String computerName = null;
String remoteAddress = request.getRemoteAddr();
System.out.println("remoteAddress: " + remoteAddress);
try {
InetAddress inetAddress = InetAddress.getByName(remoteAddress);
System.out.println("inetAddress: " + inetAddress);
computerName = inetAddress.getHostName();
System.out.println("computerName: " + computerName);
if (computerName.equalsIgnoreCase("localhost")) {
computerName = java.net.InetAddress.getLocalHost().getCanonicalHostName();
}
} catch (UnknownHostException e) {
}
System.out.println("computerName: " + computerName);
}
这篇关于在基于Servlet的Web应用程序中获取LAN客户端计算机名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!