gpt4 book ai didi

servlets - 在基于 servlet 的 Web 应用程序中获取 LAN 客户端计算机名称

转载 作者:太空宇宙 更新时间:2023-11-04 10:47:46 26 4
gpt4 key购买 nike

我有 Spring MVC 应用程序,它在 LAN 中运行。其中客户端计算机的 IP 地址会不时发生变化。因此我想获取客户端计算机名称(它们的计算机名称是固定的),因为我想获取客户端计算机的详细信息而不创建登录。

是否可以获取客户端计算机的名称?如果可以的话怎么办??或者有没有其他方法来获取用户详细信息

编辑:到目前为止我尝试过的代码

在 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 中

<init-param>
<param-name>jcifs.smb.client.username</param-name>
<param-value>username</param-value>

</init-param>

在 serverlet 类中

String username = config.getInitParameter("username");//This gives client IP address

最佳答案

我找到了获取客户端计算机名称的方法。

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);
}

关于servlets - 在基于 servlet 的 Web 应用程序中获取 LAN 客户端计算机名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48202309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com