0

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

Leos Literak
  • 7,392
  • 11
  • 59
  • 114
user2513301
  • 81
  • 1
  • 3
  • 10

2 Answers2

2

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

Community
  • 1
  • 1
Salah
  • 8,098
  • 3
  • 20
  • 39
0
<input type="button" onclick="window.location.href='/path/servlet.java'" value="Logout" class="btn" style="margin-top: 10px; margin-bottom: 10px;"/>
Selva
  • 504
  • 5
  • 10
  • 32