Jsp到servlet没有重定向

时间:2017-11-06 11:25:31

标签: java jsp

我想将主页上的submit按钮重定向到LogoutServlet servlet,但是找不到servlet 404错误页面。我还更新了web.xml文件。我不知道这里有什么问题。

  

MainPage.jsp

<form action="LogoutServlet" method="post">
<input type="submit" value="LogOut"></input></form>
  

LogoutServlet.java

package com.lfg.fc.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;

 public class LogoutServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public LogoutServlet() {
    super();

}

 public void init(ServletConfig config) throws ServletException {
    // TODO Auto-generated method stub
}


   public void destroy() {
    // TODO Auto-generated method stub
    }


  protected void doGet(HttpServletRequest request, HttpServletResponse 
   response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request,response);
    }


     protected void doPost(HttpServletRequest request, HttpServletResponse 
     response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    RequestDispatcher rd=request.getRequestDispatcher("Logout.jsp");
    HttpSession session=request.getSession(false);
    session.invalidate();
    rd.forward(request, response);
    }

     }

1 个答案:

答案 0 :(得分:0)

您必须在web.xml中输入servlet信息,如下所示:

  <servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>com.lfg.fc.web.servlets.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/LogoutServlet</url-pattern>
  </servlet-mapping>

然后在表单操作上调用您的servlet:

<form action="/LogoutServlet" method="post">
<input type="submit" value="LogOut"></input>
</form>