user2513301
user2513301

Reputation: 79

Servlet won't redirect me

i have logout button and when its pressed i want to return to main page but it stays on current page. although i receive response in chrome developer tools.

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

Upvotes: 0

Views: 403

Answers (2)

Selva
Selva

Reputation: 556

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

Upvotes: 0

Salah
Salah

Reputation: 8657

Use RequestDispatcher instead of using sendRedirect

For Example:

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

Read why and when to use each of RequestDispatcher and sendRedirect

Upvotes: 2

Related Questions