Reputation: 79
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");
Upvotes: 0
Views: 403
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
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