title: JSF 2: Navigate to external URL from a backing bean
body: <p>This is kind of an extension to the question 
  <a href="https://stackoverflow.com/questions/3297579/navigate-to-external-url-from-a-backing-bean/8609444#8609444">Navigate to external URL from a backing bean?</a></p>
  
  <p>I need to invalidate the session in the backing bean method and then forward to an external URL. The outputLink component does not have the capability to invoke an action method (logout method of the bean in this case).
  If I use commandLink, the action method gets invoked but after the session is invalidated, another session is created before forwarding to the external URL. </p>
  
  <p>To be more specific: I have a view that loads a list of items on the home page. The home page has a bunch of links (one of them being the logout image/button) on the top that come from a template xhtml. In the template xhtml file, I have the commandLink code that invokes the logout method of the LogoutBean.java. The method gets invoked fine as expected, BUT when I put a break point in the PostConstruct method of the HomeBean, I see that this method is invoked after invalidating the session and then it forwards to the external URL. How to prevent this?</p>
  
  <p>Code: </p>
  
  <p>template.xhtml:</p>
  
  <pre><code>                        &lt;h:form&gt;
                          &lt;p:panel id="HomeHeaderPanel"  styleClass="tableheaderfont" style="width:98%"&gt;
                              &lt;h:panelGrid id="filterGrid" columns="2" cellpadding="0" styleClass="normalfont" style="width: 98%" columnClasses="leftalign,rightalign"&gt;
                                  &lt;h:panelGroup&gt;
                                      &lt;h:link outcome="reviewHome.xhtml?faces-redirect=true" id="homeLink"
                                          title="Review Home"&gt;
                                          &lt;h:graphicImage value="images/home_small.png" id="homeIcon" alt="Review Home"/&gt;
                                      &lt;/h:link&gt;
                                      &lt;p:spacer width="5"/&gt;
  
                                      &lt;h:commandLink action="#{logoutBean.logout}" &gt;  
                                          &lt;h:graphicImage value="images/logout_small.png" id="logoutIcon" alt="Logout" title="Logout"/&gt;
                                      &lt;/h:commandLink&gt;  
  
                                  &lt;/h:panelGroup&gt;
                              &lt;/h:panelGrid&gt;
                          &lt;/p:panel&gt;
                      &lt;/h:form&gt;
  </code></pre>
  
  <p>reviewHome.xhtml:</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.prime.com.tr/ui"&gt;
  &lt;f:view contentType="text/html"&gt;
  &lt;ui:composition template="template.xhtml"&gt;
      &lt;ui:define name="content"&gt;  
                &lt;h:form styleClass="form" id="ptpReviewForm"&gt;
  
      &lt;f:event listener="#{homeBean.refresh}" type="preRenderView" /&gt;...content here...
  &lt;/h:form&gt;
  &lt;/ui:define&gt;
      &lt;/ui:composition&gt;
      &lt;/f:view&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>HomeBean.java:</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class HomeBean implements Serializable{
  
  /**
   * 
   */
  private static final long serialVersionUID = 3303546811067129792L;
  public HomeBean(){}
  
  @EJB
  private ReviewEJB reviewEJB;
  private List&lt;HomeObject&gt; homeList;
  
  
  @SuppressWarnings("unused")
  @PostConstruct
  private void populateItpList()
  {
      logger.debug("In the PostConstruct method");
      homeList = reviewEJB.getList();
  
  }
  
  //This method is loaded every time the Review Home view loads (prerenderView event)
  public void refresh()
  {
      String refresh = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("refresh");
      if(null != refresh &amp;&amp; refresh.equalsIgnoreCase("true"))
      {
          homeList = reviewEJB.getList("another");
      }
  } 
  </code></pre>
  
  <p>LogoutBean.java:</p>
  
  <pre><code>@ManagedBean
  @SessionScoped
  public class LogoutBean {
  
  
  public void logout() throws ServletException
  {
  
      try {
          ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
          ((HttpServletRequest) ec.getRequest()).logout();
          HttpSession session = (HttpSession) ec.getSession(false);
  
          // invalidate session
          if (session != null) {
              session.invalidate();
          }
          ec.redirect("/anotherWebAppContext/forward.jsp?link=review");
  
      }  catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>