This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Logout Code is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Logout Code is not working" Watch "Logout Code is not working" New topic
Author

Logout Code is not working

Stephen Wei
Ranch Hand

Joined: Oct 09, 2001
Posts: 73
Hi, I am assuming user use IE browser which supports Javascript. I want my LogoutServlet to run when the user pushes the logout button or close the browser. Here is my code, but my servlet will not get executed, my browser closes ok(I want the browser to close when the user logout), why is this? When I remove the "window.close();" line, my servlet will execute, is it because my window closes too quickly before it submits the form?
< script language = "javascript">
function logOut()
{
window.event.returnValue = false;
if(confirm("logout?")){
window.event.returnValue = true;
window.close();
}
}
/script
< p
align="center"> < input type="submit" value="logout" action="http/myhost/Servlet/LogoutServlet"
"onclick"="logOut()"> //I put an extra quotation here because other wise I can not submit this message to Javaranch, strange.
< /p>

Also, can someone explain the "onclose()" function to me? How do I submit a request to the servlet using only Javascript? something like:
Window.onclose(){
Document.myform.submit();
} /
???
Thanks!


Sun Certified Programmer for Java Platform
Kenneth Reising
Greenhorn

Joined: Jul 30, 2001
Posts: 10
Your essentially correct, the onClick event happens faster then the browser is able to form a new HTTP request and call your logout servlet.
The onClose() javascript function does pretty much what you think it does. When the window closes, do something.
However, your example will not work, becuase when the window closes, there is no longer a javascript Document.myForm object to submit.

The best workaround would be to have your logout servlet close the page instead, plus that way you can properly handle any exceptions that may occur in the process of logging off the user.
 
 
subject: Logout Code is not working
 
Similar Threads
perform a specific task on logout/closing of browser
Logout using HttpSession
Enctype Error
How to stop browser pange closing without showing confirm dialog box
logOut script not working