When user clicked on Logout button/link in JSF page, we can call Javascript to close browser window.
This can be achieved through 2 different ways.
1. We can directly call JavaScript using clientlistener for command button. This method more faster.
2. We can call Java method to execute Javascript using RenderKitService API. This method allows to perform any logic on server side during logout.
Following Sample Code Snippets will help you to do it.
JSF Page Source:
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<f:view>
<af:document id="d1">
<af:resource type="javascript">
function closeWindow(evt) {
window.close();
}
</af:resource>
<af:form id="f1">
<af:panelStretchLayout id="psl1">
<f:facet name="center">
<!-- id="af_one_column_stretched" -->
<af:panelGroupLayout layout="vertical"
xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
id="pgl1">
<af:outputText value="Hello There! This is a Sample Page to test browser close on clicking Logout button"
id="ot1"
inlineStyle="font-weight:bolder; font-size:small;"/>
<af:spacer id="s1" height="30" width="20"/>
<af:outputText value="Clicking on --Logout1-- Button will call Java bean method to execute Javascript uisng Renderkit"
id="ot2" inlineStyle="color:Green;"/>
<af:commandButton text="Logout1" id="cb1"
action="#{pageFlowScope.signOutBean.logOutApp}"
partialSubmit="true"/>
<af:spacer id="s2" height="20" width="20"/>
<af:outputText value="Clicking on --Logout2-- Button will call Javascript written on page"
id="ot3" inlineStyle="color:Maroon;"/>
<af:commandButton text="Logout2" id="cb2" immediate="true">
<af:clientListener method="closeWindow" type="action"/>
</af:commandButton>
</af:panelGroupLayout>
</f:facet>
</af:panelStretchLayout>
</af:form>
</af:document>
</f:view>
</jsp:root>
Managed Bean Source:
import java.io.IOException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
import weblogic.servlet.security.ServletAuthentication;
public class signOut {
public signOut() {
super();
}
public String logOutApp() {
// Add event code here...
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext ectx = facesContext.getExternalContext();
//String url = ectx.getRequestContextPath() + "/adfAuthentication?logout=true&end_url=/faces/logout.jspx";
ExtendedRenderKitService service = Service.getRenderKitService(facesContext,ExtendedRenderKitService.class);
service.addScript(facesContext, "window.close();");
HttpSession session = (HttpSession)ectx.getSession(false);
session.invalidate();
HttpServletRequest request = (HttpServletRequest)ectx.getRequest();
ServletAuthentication.logout(request);
ServletAuthentication.invalidateAll(request);
ServletAuthentication.killCookie(request);
// try {
// ectx.redirect(url);
// } catch (IOException e) {
// e.printStackTrace();
// }
// facesContext.responseComplete();
return null;
}
}