我有一个锚点,我希望它链接到LogoutServlet,以便它会破坏会话并将其重定向回登录页面。
LogoutServlet.java
package pkg;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LogoutServlet
*/
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LogoutServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setHeader("Cache-Control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
request.getSession().invalidate();
RequestDispatcher rd = request.getRequestDispatcher("Login.html");
rd.forward(request, response);
}
}
标签
<a href="/Assignment/LogoutServlet" accesskey="1" title="">Logout</a>
这是实施它的正确方法吗?我用过这个,但它没有将我重定向到Login.html。
答案 0 :(得分:9)
这将使用doGet
方法而不是doPost
方法。这样的锚链接是HTTP GET请求。
如果您希望发出POST请求,则需要使用POST方法向servlet提交表单。
将代码移至doGet
而不是doPost
,然后尝试。
答案 1 :(得分:1)
使用doGet方法。一个href将使用GET方法。