Get LAN client machine name in servlet based web application

Neha picture Neha · Dec 10, 2013 · Viewed 9k times · Source

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

Answer

Neha picture Neha · Jan 8, 2014

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