JavaBlog.fr / Java.lu DEVELOPMENT,Java,Spring,WEB Java/Spring/Web: Logout servlet example

Java/Spring/Web: Logout servlet example

Hi,

A simple mini-post concerning a servlet to invalidate the current session when the user wants to logout. This is directly supported by the servlet api by the call to appropriate method HttpSession.invalidate() in a servlet or a controller dedicated to invalidate session.

I). Server Side components
First, following the codes of coordinator servlet LogoutServlet:

01public class LogoutServlet implements org.springframework.web.HttpRequestHandler {
02     
03    // -------------------------------------------------------------- PUBLIC FUNCTIONS
04    public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
05        HttpSession session = req.getSession(false);
06 
07         
08        res.setHeader("Cache-Control","no-cache");
09        res.setHeader("Cache-Control","no-store");
10        res.setDateHeader("Expires", 0);
11        res.setHeader("Pragma","no-cache");
12 
13        if (session != null) {
14            session.invalidate();
15        } // end-if
16 
17        PrintWriter out = res.getWriter();
18        out.println("<html><body><script>window.close();</script></body></html>");
19    }
20}

Explanations:

  • This class LogoutServlet implements the interface HttpRequestHandler which is an interface for the creation of coordinator servlet i.e. plain handler interface for components that process HTTP requests, analogous to a Servlet. In this example, because we use this solution in a Web application based on Spring MVC, we will use the option recommended by the Spring (2) documentation for the way of exposing an HttpRequestHandler:

    (1) The easiest way to expose an HttpRequestHandler bean in Spring style is to define it in Spring’s root web application context and define an HttpRequestHandlerServlet in web.xml, pointing at the target HttpRequestHandler bean through its servlet-name which needs to match the target bean name.

    (2) Supported as a handler type within Spring’s DispatcherServlet, being able to interact with the dispatcher’s advanced mapping and interception facilities. This is the recommended way of exposing an HttpRequestHandler, while keeping the handler implementations free of direct dependencies on a DispatcherServlet environment.

  • the web.xml file will be:
    01[...]
    02    <servlet>
    03        <servlet-name>myHUODispatcherServlet</servlet-name>
    04        <servlet-class>
    05            org.springframework.web.servlet.DispatcherServlet
    06        </servlet-class>
    07        <load-on-startup>1</load-on-startup>
    08        <!-- Configuration file of dispatcher servlet is: myHUODispatcherServlet-servlet.xml  -->
    09    </servlet>
    10 
    11    <servlet-mapping>
    12        <servlet-name>myHUODispatcherServlet</servlet-name>
    13        <url-pattern>*.do</url-pattern>
    14    </servlet-mapping>
    15[...]
  • …and myHUODispatcherServlet-servlet.xml file:
    01[...]
    02    !-- ################### SPRING MVC CONTROLLER XML  ################### -->
    03    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    04        <property name="mappings">
    05            <value>
    06                /loginSecure.do=loginSecureController
    07                /myController.do=myControllerDo
    08                /logout.do=LogoutServlet
    09                        </value>
    10        </property>
    11    </bean>
    12 
    13    <!-- ################### LOGOUT SERVLET ######################### -->
    14    <bean id="LogoutServlet" class="ec.ep.qe2web.site.servlet.LogoutServlet"></bean>
    15[...]

II). Client Side
Here, an example of a SENCHA/ExtJs interface containing a link:

1{
2    text: 'Logout',
3    handler: function(grid, rowIndex, colIndex) {
4        document.location='logout.do';
5    }
6}

Note: Often, a more simple solution could be directly close the current window, however, this solution doesn’t invalidate the session:

1{
2    text: 'Logout',
3    handler: function(grid, rowIndex, colIndex) {
4        window.close();
5    }
6}

That’s all!!!

Huseyin OZVEREN

1 thought on “Java/Spring/Web: Logout servlet example”

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post

Documentum : BOF – SBO (Service based Business Object) – NoSuchMethodException – JAVA reflectionDocumentum : BOF – SBO (Service based Business Object) – NoSuchMethodException – JAVA reflection

Hello, After my previous post http://www.javablog.fr/documentum-bof-sbo-service-based-business-object-tbo-type-based-business-object-aspects.html, just a mini post related an error java.lang.NoSuchMethodException which occurs when the use of a SBO’s method whereas it exists. Basically in JAVA, the

Java : SSL : Tool KeyStore Explorer, JDK cacerts truststore, Disable Certificate Validation in Java SSL ConnectionsJava : SSL : Tool KeyStore Explorer, JDK cacerts truststore, Disable Certificate Validation in Java SSL Connections

Hello, After my first post concerning the SSL and the tool PorteCle (http://www.javablog.fr/java-ssl-generate-keystore-self-signed-certificate-tool-portecle.html) allowing the generation of KeyStore, self-signed certificate instead of Keytool supported in the JDK / JRE, I