18 October 2012

How To Get IP Address for Logging In Java


InetAddress thisIp = null;
try {
thisIp = InetAddress.getLocalHost();

// Get IP Address
   byte[] ipAddr = thisIp.getAddress();

   // Get hostname
   String hostname = thisIp.getHostName();

   System.out.println("Local host IP :"+thisIp.getHostAddress());
   System.out.println("getAddress : "+ipAddr);
   System.out.println("getHostName : "+hostname);
 
} catch (UnknownHostException e1) {
e1.printStackTrace();
}

How To Get URL Context Path In Java

HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String schema = httpServletRequest.getScheme();
String serverName = httpServletRequest.getServerName();
int serverPort = httpServletRequest.getServerPort();
String contextPath = httpServletRequest.getContextPath();
String path = schema + "://" + serverName + ":" + serverPort + contextPath;
System.out.println("Path : "+path);