Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Logout Code is not working

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic