Get LAN client machine name in servlet based web application

后端 未结 2 608
自闭症患者
自闭症患者 2020-12-20 05:53

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

相关标签:
2条回答
  • 2020-12-20 06:34

    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.

    0 讨论(0)
  • 2020-12-20 06:40

    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);
    }    
    
    0 讨论(0)
提交回复
热议问题