我有注销按钮,按下它后我想返回主页但它仍然在当前页面上。虽然我在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");
答案 0 :(得分:2)
使用RequestDispatcher
代替sendRedirect
例如:
RequestDispatcher reqDispatcher = req.getRequestDispatcher("pathToResource/MyPage.jsp");
reqDispatcher.forward(req,res);
的原因和时间
答案 1 :(得分:0)
<input type="button" onclick="window.location.href='/path/servlet.java'" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/>