Sunday, September 1, 2013

Retrieve IP address from JSF

One of my requirement in audit trail module is to capture the IP address of the user accessing the web application. My web application was developed using JSF, may I know could this be done? Such an easy job, below is the code for this mission:
 HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
 String ipAddress = request.getHeader("X-FORWARDED-FOR");
 if( ipAddress == null ) {
  ipAddress = request.getRemoteAddr();
 }

No comments: