Question

i am working on a spring mvc project and i have used Tomcat FORM authentication for login. and the application is working fine so now i am working on the logout implementation. the application is having home page (home.jsp) with logout link and i am trying to call a LogoutServlet as below.

LogoutServlet.java

@WebServlet(urlPatterns={"/logout"})
public class LogoutServlet extends HttpServlet  {

protected void doGet(HttpServletRequest  request,HttpServletResponse response) 
      throws ServletException, IOException {

      // To logout the current user
      request.getSession().invalidate();

    // The user is now logged out so redirect the user to the welcome.jsp page .
    response.sendRedirect(request.getContextPath() + "/welcome.jsp");
  }

}

home.jsp

<body>      

    <a href="LogoutServlet">Logout</a>

        <form:form id="formDataBean" method="post" modelAttribute="formDataBean" action="submit" class="form" >

        //form body
        .
        .

        </form:form>
</body> 

web.xml

<welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml,classpath*:gameResult-context.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <security-constraint>

    <web-resource-collection>
      <web-resource-name>Admin</web-resource-name>
      <url-pattern>/*</url-pattern>
      <http-method>GET</http-method>
      <http-method>POST</http-method>
    </web-resource-collection>

    <auth-constraint>
      <role-name>tomcat</role-name>
    </auth-constraint>

    <user-data-constraint>
      <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>

  </security-constraint>

  <login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
      <form-login-page>/login</form-login-page>
      <form-error-page>/login-failed</form-error-page>
    </form-login-config>
  </login-config>



 // i have a doubt on the below code as i don't know if it has to be there at all
 // because when ever i start the application (server) this servlet gets initialize 
 // and doGet() gets execute.

 <servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>org.sample.servlets.LogoutServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/logout/*</url-pattern>
  </servlet-mapping>

</web-app>  

here is the problem : i want to call the LogoutServlet from logout link and redirect to welcome.jsp page but right now its not working, its throwing HTTP Status 404 error (http://localhost:8080/gameResult/LogoutServlet).

Was it helpful?

Solution

According to your Servlet, you should probably call http://localhost:8080/gameResult/logout instead.

<a href="logout">Logout</a>

You could add automatically context to elimitate further problems if you use this code in another path, this way you won't have to bother about relative path.

<a href="${pageContext.request.contextPath}/logout">Logout</a>

Also, you should remove this stuff from web.xml :

<servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>org.sample.servlets.LogoutServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/logout/*</url-pattern>
</servlet-mapping>

Because you are using WebServlet annotation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top