【问题标题】:calling Servlet from jsp <href> link is not working从 jsp <href> 链接调用 Servlet 不起作用
【发布时间】:2014-01-22 03:00:31
【问题描述】:

我正在开发一个 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");
  }

}

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>  

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

【问题讨论】:

    标签: jsp tomcat spring-mvc servlets logout


    【解决方案1】:

    根据您的 Servlet,您可能应该调用 http://localhost:8080/gameResult/logout

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

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

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

    另外,你应该从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>
    

    因为你使用的是WebServlet注解。

    【讨论】:

    • 非常感谢@Alexandre 的回答,我使用了 Logout 并从 web.xml 中删除了 LogoutServlet并且工作正常。
    • @Soumyaansh 不要忘记回答您的问题并标记为已回答!
    • 为什么我们需要添加 contextPath ${pageContext.request.contextPath} href 将自己生成一个 GET 请求,所以 web 容器应该为给定的 url-pattern 调用适当的 servlet是吗?你能解释一下区别吗??
    • 因为href是相对的,这意味着它将通过将“注销”附加到当前url来加载url。如果您位于http://localhost/context/somepath/,它将加载http://localhost/context/somepath/logout,但url-pattern 将仅匹配http://localhost/context/logout,因此您要么永远不会更改路径(始终引用context-path,要么使用${pageContext.request.contextPath} 提供完整路径.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2011-07-08
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多