Get LAN client machine name in servlet based web application

9.8k views Asked by At

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

Edit: codes I have tried so far

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
}

Edit: reply to @Eugeny Loy In web.xml

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

</init-param>

In serverlet class

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

There are 2 answers

1
Neha On BEST ANSWER

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);
}    
1
Eugene Loy On

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.