18 October 2012

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