Hi Everyone.
I have a problem getting the client's computer name. (Actually I don't know how, and can't find it either in google search)
This is a Web project.
Below is a example code:
import java.net.InetAddress; import java.net.UnknownHostException; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.servlet.http.HttpServletRequest; @ViewScoped @ManagedBean(name = "domain") public class DomainTest extends BasePageBean { public void testDomain() throws UnknownHostException { HttpServletRequest request = getRequest(); // HttpServlet request created in BasePageBean class String ipAddress = request.getHeader("X-Real-IP"); if (ipAddress == null) { ipAddress = request.getRemoteAddr(); } System.out.println(ipAddress); System.out.println("Local Address " + request.getLocalAddr()); System.out.println("User " + request.getRemoteUser()); System.out.println("Host " + request.getRemoteHost()); InetAddress i = InetAddress.getByName(request.getRemoteAddr()); System.out.println("Inet Address " + i); System.out.println("Computer Name " + i.getHostName()); } }
The BasePageBean is also a java program that Initialized the HttpServletRequest and HttpServletResponse.
I just need to inherit it for each page in the web project. I hope you get it
The problem is, once i deployed the project in our UNIX server, and run it through my computer, the output I got is always IP Address
Sample output:
Local Address 10.123.156.22
User null
Host 10.190.22.76
Inet Address /10.190.22.76
Computer Name 10.190.22.76
---------------------------------------------------------------------------------
That kind of algorithm will work if it is not a web project. For example a simple java program that you can run in your machine
Example Code:
Above code will give an output of:import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; public class TestDomain { public static void main(String[] args) throws SocketException { try { InetAddress i = InetAddress.getByName("10.190.22.76"); System.out.println(i); System.out.println(i.getHostAddress()); System.out.println(i.getHostName()); System.out.println(i.getCanonicalHostName()); } catch (UnknownHostException ex) { Logger.getLogger(TestDomain.class.getName()).log(Level.SEVERE, null, ex); } } }
/10.190.22.76
10.190.22.76
10ST-BFUENTES.smart.LOCAL
10ST-BFUENTES.smart.LOCAL
Since its getting the info from where the program runs.
So I'm having a problem once I implement it through web and deploy it to our UNIX server.
If the above algorithm is capable of getting the username of client's machine please help me.
If not, is there any other way of getting the username of client's machine? thank you!
I need it so that our web project can log in the client using the client's username..