JSF 2 Redirection

Tuesday, 29 January 2013 16:10

JSF 2 redirection with Java Server Faces

External Site redirection with JSF

To redirect to an external site you need to get an external context using FaceContext then call the redirect method with the external website url

Java Server Faces external site redirection


ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect("htp://www.java-tutorial.ch");

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.

Return the absolute context path from your Backing bean


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

Tags: site , method , redirection , redirect , external , website , externalcontext , //www.java-tutorial.ch , facescontext.getcurrentinstance().getexternalcontext(); , externalcontext.redirect(

Add comment


Security code
Refresh