Get LAN client machine name in servlet based web application

こ雲淡風輕ζ 提交于 2019-11-29 12:50:54

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

Is that possible to get client machine's name??

You're probably referring to NetBIOS name here. If that's the case - you should use some library that implements NetBIOS/SMB/CIFS in java to do this.

if it's possible how??

Have a look on JCIFS. I won't give you the exact code snippet but this is the direction you should move to solve this.

Or is there any other way to get that user details

As far as I understand your problem, what you need is a way to identify host and you cannot rely on IP address for that.

If that's the case one of the other options would be using MAC address, but you'll probably wont be able to do this with pure java since this is more low-level protocol java normally deals with, so it will probably be less portable. This tutorial might help.

UPDATE

I come across NetBIOS/SMB/CIFS stack but I haven't worked with it in Java and JCIFS. That's why I won't give you specific code piece that will solve your issue but rather direction where you should look.

Check out NbtAddress class docs. Seems to be what you are looking for. Also check out the examples to get the idea how it can be used.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!