001    package com.sptci.echo2.http;
002    
003    import java.io.IOException;
004    
005    import javax.servlet.ServletException;
006    import javax.servlet.http.HttpServlet;
007    import javax.servlet.http.HttpServletRequest;
008    import javax.servlet.http.HttpServletResponse;
009    
010    /**
011     * A servlet used to implement logout feature for Echo2 applications.
012     * Invalidates the user session and redirects the user to a page configured
013     * as a <code>init-param</code> for the servlet.
014     * 
015     * <p>Copyright 2006 Sans Pareil Technologies, Inc.</p>
016     * 
017     * @author Rakesh Vidyadharan 2006-06-25
018     * @version $Id: LogoutServlet.java 3217 2007-05-07 11:06:01Z rakesh $
019     */
020    public class LogoutServlet extends HttpServlet
021    {
022      /**
023       * The name of the servlet <code>init-param</code> that holds the value
024       * of {@link #url}.
025       */
026      private static final String URL_PARAM = "redirectUrl";
027    
028      /**
029       * The path to which the user should be redirected to after logging
030       * out of the current session.
031       */
032      private String url;
033    
034      /**
035       * Initialise this servlet instance.
036       */
037      @Override
038      public void init()
039      {
040        url = getInitParameter( URL_PARAM );
041      }
042    
043      /**
044       * Process HTTP requests.
045       *
046       * @see #invalidate
047       */
048      protected void service( HttpServletRequest request,
049        HttpServletResponse response ) throws ServletException, IOException
050      {
051        invalidate( request );
052        response.sendRedirect( url );
053      }
054    
055      /**
056       * Invalidate the current HTTP session and display a standard page
057       * that lets the user restart a new session.
058       */
059      protected void invalidate( HttpServletRequest request )
060      {
061        request.getSession().invalidate();
062      }
063    }