Get LAN client machine name in servlet based web application

≡放荡痞女 提交于 2019-11-28 06:07:38

问题


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

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/20492186/get-lan-client-machine-name-in-servlet-based-web-application

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