我有 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/