我有注销按钮,按下该按钮后我想返回主页,但它保留在当前页面上。尽管我在 Chrome 开发者工具中收到了响应。
userinfo.jsp
<input type="button" onclick="logout()" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/>
logout.js
function logout(){
$.post("Logout");
}
Logout.java servlet
public class Logout extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = null;
try{
HttpSession sess = request.getSession();
if(sess != null){
sess.invalidate();
}
response.sendRedirect("index.html");
最佳答案
使用RequestDispatcher
而不是使用sendRedirect
例如:
RequestDispatcher reqDispatcher = req.getRequestDispatcher("pathToResource/MyPage.jsp");
reqDispatcher.forward(req,res);
阅读为什么以及何时使用 RequestDispatcher
and sendRedirect
中的每一个
关于java - Servlet 不会重定向我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23406244/