Calling a servlet /logout from a template.xhtml

FabRoussel

I would like to call a servlet /logout from a templateHeader.xhtml which is located in the folder templates as followed:

webapp
 |-- form
 |    |-- form.xhtml
 |-- WEB-INF
 |    |-- templates
 |    |    |-- template.xhtml
 |    |    |-- templateFooter.xhtml
 |    |    |-- templateHeader.xhtml
 |-- resources
 |-- admin.xhtml
 |-- login.xhtml
 :

The problem is that I don't know how to call the servet because the path to servlet is not the same depending on the page you are on. I am looking for an equivalent of #{request.contextPath}/myPage but for the servlet. And just by curiosity, if I wanted to call a method myMethod() from a bean Login, how would I do?

I have followed this Kill session and redirect to login page on click of logout button but I think I am using it wrong. Note that I have also tried to add method="post" to the menuitem. Note also that the first menuitem of the code below is working.

templateHeader.xhtml

<ui:composition 
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
	<p:layoutUnit id="top" position="north" size="50">
		<h:form>	
			<p:menubar>
				<p:menuitem value="Satisfact'IT" url="#{request.contextPath}/admin.xhtml" icon="fa fa-home" />
				<p:menuitem value="Quitter" action="${pageContext.request.contextPath}/logout" icon="fa fa-sign-out"/>
			</p:menubar>
		</h:form>
	</p:layoutUnit>
</ui:composition>

template.xhtml

<?xml version='1.0' encoding='ISO-8859-1' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      >    
    <h:head>
	</h:head>
    <h:body>
		<p:layout fullPage="true" >   
		    <ui:insert name="header" >
		        <ui:include src="commonHeader.xhtml" />
		    </ui:insert>
        
		    <ui:insert name="footer" >
		        <ui:include src="commonFooter.xhtml" />
		    </ui:insert>
		</p:layout>
    </h:body>
</html>

And finally the logout servlet

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath() + "/login.xhtml");
    }
}

Melloware

A couple of things I think you want to do.

Create a normal JSF controller (e.g LogoutController) method like...

public String logout() {
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    session.invalidate();
    return "/login.xhtml?faces-redirect=true";
}

Change your menu item to..

<p:menuitem value="Quitter" action="${logoutController.logout}" icon="fa fa-sign-out"/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling Servlet from JavaScript

From Java

Calling Servlet Post from another Servlet

From Java

calling a java servlet from javascript

From Java

Calling a Servlet from a Java application

From Dev

Calling a Rest Service from a servlet

From Dev

How to get a value from a servlet to jsf-page (xhtml)

From Dev

calling servlet from php page using ajax

From Java

Calling a servlet from JSP file on page load

From Dev

Calling Firebird Stored procedure from Java Servlet

From Dev

calling Servlet from jsp <href> link is not working

From Dev

During Calling a servlet from another Servlet that contains session

From Dev

Calling component function from template

From Dev

Automatic logout by calling the logout endpoint

From Dev

Calling a static template method from a template function

From Dev

Calling a template function from a template function in a class

From Dev

Logout using servlet

From Dev

Calling a template variable from an output modifier in Modx?

From Dev

Calling Rest API from a markdown template

From Dev

Calling a template function from a C file

From Dev

Calling service method from a directive template

From Dev

Calling a function from a helper in Vue Template

From Dev

jQuery Kendo Grid calling a function from template

From Dev

Calling template function from inner class

From Dev

Calling database from a Laravel blade template

From Dev

Calling ARM Template from C#

From Dev

Multiple times method calling from angular template

From Dev

Calling Django JSON API from Template

From Dev

Calling servlet from a JSP page's form action

From Dev

calling a java servlet from javascript ajax post method

Related Related

HotTag

Archive