1

created session from login.jsp page using servlet

String msg = ""; HttpSession sess = request.getSession(); // if(sess != null) //sess.invalidate(); if (sess.getId() != null) { sess.setAttribute("uname", uname); sess.setAttribute("pwd", pwd); } 

retrived session in other jsp page using

<b> Welcome ${uname}</b> 

logout hyperlink

<a href="login_ml.jsp" id="logout_link" onclick='lgt()'>Logout</a></td> 

javascript to clear session

function lgt(){ var logout = document.getElementById("logout_link"); logout.session.clear(); alert("logout"); } 
5
  • use sess.invalidate(); Commented Oct 20, 2015 at 9:40
  • but javascript is in other .js file. Commented Oct 20, 2015 at 9:43
  • Your client side will need to make a server request to invalidate the session on server side. Commented Oct 20, 2015 at 9:45
  • The Javascript merely gets the a element in the HTML whose id is logout_link. Why do you expect an a element to have a session attribute? Commented Oct 20, 2015 at 9:45
  • can you give one example? how to do this? Commented Oct 20, 2015 at 9:46

1 Answer 1

2

We cannot clear session directly from JS code . You have to call another JSP page will invalidate that session :

Javascript Function :

 function destroySession() { window.location = "killSession.jsp"; } 

killSession.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Log out</title> </head> <body> <% session.invalidate(); %> User has been Logged out sucessfully!!!! </body> </html> 
Sign up to request clarification or add additional context in comments.

7 Comments

i just called that javascript using <a href="login_ml.jsp" id="logout_link" onclick='destroySession()'>Logout</a></td> but it is not working
is it calling the JPS page ? what you have written in JSP /
<a href="login_ml.jsp" id="logout_link" onclick='destroySession()'>Logout</a> this is the link i have written in jsp.
No. I am talking about killSession.jsp. xmlhttp.open("GET","killSession.jsp",false);
hey, @Rehman i got you.. i tried using "page1.jsp1" which is the name of the jsp page i have called the session using ${uname}.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.