title: Call Javascript function in Servlet, after a submit Button has been pressed
body: <p>I have got a question. I know how to execute JavaScript in a servlet, but I am having a problem when I want to execute it right after I did redirect the user to another side. So here is the Code, which shows my problem. I am using a submit button on the html side.</p>
  
  <pre><code>PrintWriter out = response.getWriter();  
  response.setContentType("text/html");  
  out.println("&lt;script type=\"text/javascript\"&gt;");  
  out.println("alert('the session did time out, please reconnect');");  
  out.println("logout();");
  out.println("&lt;/script&gt;");    
  request.getRequestDispatcher("/Login.jsp").forward(request, response);
  </code></pre>
  
  <p>what happens now is, i will be redirected to my login page, but i do not see any alert.
  But when i delete the forwarding line and there will be written </p>
  
  <pre><code>PrintWriter out = response.getWriter();  
  response.setContentType("text/html");  
  out.println("&lt;script type=\"text/javascript\"&gt;");  
  out.println("alert('the session did time out, please reconnect');");  
  out.println("logout();");
  out.println("&lt;/script&gt;"); 
  // the redirect is missing now
  </code></pre>
  
  <p>So now I am getting the alert message, but I am getting redirected to a blank page, because there was no redirect at all. </p>
  
  <p>So now I am trying to create an alert, which tells the user something in an alert. Right after it, I want to redirect him to the loginpage, where I want to call the 'logout' function which I wrote.</p>
  
  <p>But neither I am getting an alert nor does the logout function get called.
  Based on <a href="https://stackoverflow.com/questions/4064605/java-servlets-open-message-popup">this</a> question this should work</p>