Get the asbolute context path from your backing bean
To return your absolute context path in your backing you can use the following snippet. This method take into account the fact that you are using a "non standard" port by adding the port only if you are using a port different from the usual 80.
public String getAbsoluteContextPath(){
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
StringBuffer buffer = new StringBuffer();
buffer.append(request.getScheme()); //http,https,...
buffer.append("://");
buffer.append(request.getServerName()); //localhost
if (request.getServerPort() != 80){
buffer.append(":"); //localhost
buffer.append(request.getServerPort()); //8080
}
if (request.getContextPath() !=""){
buffer.append("/");
buffer.append(request.getContextPath());
}
else
buffer.append("/");
return buffer.toString();
}
If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on