Servlet不会重定向我

时间:2014-05-01 10:52:29

标签: java jquery jsp servlets

我有注销按钮,按下它后我想返回主页但它仍然在当前页面上。虽然我在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");

Here's Response

2 个答案:

答案 0 :(得分:2)

使用RequestDispatcher代替sendRedirect

例如:

RequestDispatcher reqDispatcher = req.getRequestDispatcher("pathToResource/MyPage.jsp");
reqDispatcher.forward(req,res);

了解使用RequestDispatcher and sendRedirect

的原因和时间

答案 1 :(得分:0)

<input type="button" onclick="window.location.href='/path/servlet.java'" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/>