从jsp <href>链接调用Servlet不起作用

Cor*_*ral 2 jsp tomcat servlets spring-mvc logout

我正在开发一个spring mvc项目,我已经使用Tomcat FORM身份验证进行登录.并且应用程序工作正常,所以现在我正在进行注销实现.应用程序正在具有注销链接的主页(home.jsp),我正在尝试调用LogoutServlet,如下所示.

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");
  }
Run Code Online (Sandbox Code Playgroud)

}

针对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> 
Run Code Online (Sandbox Code Playgroud)

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>  
Run Code Online (Sandbox Code Playgroud)

这是问题所在: 我想从logout链接调用LogoutServlet并重定向到welcome.jsp页面但是现在它无法正常工作,它抛出HTTP状态404错误(http://localhost:8080/gameResult/LogoutServlet).

Ale*_*oie 6

根据你的Servlet,你可能应该打个电话http://localhost:8080/gameResult/logout.

<a href="logout">Logout</a>
Run Code Online (Sandbox Code Playgroud)

如果您在另一条路径中使用此代码,则可以自动添加上下文以消除进一步的问题,这样您就不必担心相对路径.

<a href="${pageContext.request.contextPath}/logout">Logout</a>
Run Code Online (Sandbox Code Playgroud)

此外,你应该删除这些东西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>
Run Code Online (Sandbox Code Playgroud)

因为您正在使用WebServlet注释.