Java Servlet赢得';不要改变我的方向
我有注销按钮,当它按下时,我想返回主页,但它停留在当前页面上。尽管我在chrome开发者工具中收到了回应 userinfo.jspJava Servlet赢得';不要改变我的方向,java,jquery,jsp,servlets,Java,Jquery,Jsp,Servlets,我有注销按钮,当它按下时,我想返回主页,但它停留在当前页面上。尽管我在chrome开发者工具中收到了回应 userinfo.jsp <input type="button" onclick="logout()" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/> Logout.java servlet public class Logout extends HttpServlet {
<input type="button" onclick="logout()" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/>
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);
阅读使用每种代码的原因和时间
您收到了什么样的响应?tehre的图片在上面我不太了解jquery,但我认为这个调用应该对行为负责。你能在没有javascript的情况下测试正常表单吗?$.post(“注销”)
是一个ajax调用。为什么页面会重定向?如果在被Ajax命中的servlet中重定向,则不会重定向发送Ajax请求的页面。它只重定向Ajax响应。您甚至没有阅读对Ajax请求的响应!如果OP想要重定向,这是一个怎样的答案?我也使用了dispatcher而不是redirect。我同意,问题出在客户端,这个答案与问题无关。
RequestDispatcher reqDispatcher = req.getRequestDispatcher("pathToResource/MyPage.jsp");
reqDispatcher.forward(req,res);
<input type="button" onclick="window.location.href='/path/servlet.java'" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/>