Tuesday, June 30, 2015

Handling logout mechanism in J2EE way

Last time when I was working with Spring, the logout was handled in such a way:
  <http auto-config="true">
    ...

    <logout logout-success-url="/pages/login.xhtml?faces-redirect=true">
  </http>
But now I'm doing it in pure J2EE way, thus this is what I got:
 <h:form>
  <p:commandbutton action="#{myController.logout}" value="logout" />
 </h:form>
@ManagedBean
@RequestScoped
public class MyController {

 ...
 ...
 
 public void logout() throws IOException {
  ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
  ec.invalidateSession();
  ec.redirect(ec.getRequestContextPath() + "/login.xhtml");
 }
}
Notice the huge difference? With Spring, one statement rules the world.

No comments: