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.
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<script type=\"text/javascript\">");
out.println("alert('the session did time out, please reconnect');");
out.println("logout();");
out.println("</script>");
request.getRequestDispatcher("/Login.jsp").forward(request, response);
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
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<script type=\"text/javascript\">");
out.println("alert('the session did time out, please reconnect');");
out.println("logout();");
out.println("</script>");
// the redirect is missing now
So now I am getting the alert message, but I am getting redirected to a blank page, because there was no redirect at all.
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.
But neither I am getting an alert nor does the logout function get called. Based on this question this should work
what happens now is, i will be redirected to my login page, but i do not see any alert.
Take a look at the javadoc for RequestDispatcher#forward().
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
So when you forward() , the current response is not committed to client and hence your html and javascript code don't run. Forward means your current Servlet is not handling the request, the forwarded code should handle it instead and hence you don't see anything written in the out.println() inside current Servlet as part of final response. Tjis is forwarding the request and is done in the Server side itself, this is not redirection which is initiated by the browser . To redirect you need to use HttpServletResponse#sendRedirect.
So now i am getting the alert message, but i am getting redirected to a blank page
You are not getting redirected to any page , examine closely what you have written to the response :
out.println("<script type=\"text/javascript\">");
out.println("alert('the session did time out, please reconnect');");
out.println("logout();");
out.println("</script>");
In this case, browser is fed with a HTML code something like this :
<html>
<script type="text/javascript">
alert('the session did time out, please reconnect');
logout();
</script>
This code will just show an alert and call logout() function if any. And it displays the HTML page which doesn't have any visual content in its current form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With