java 按下提交按钮后,在 Servlet 中调用 Javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16478748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:04:54  来源:igfitidea点击:

Call Javascript function in Servlet, after a submit Button has been pressed

javaservletspost

提问by SomeJavaGuy

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.

我有一个问题。我知道如何在 servlet 中执行 JavaScript,但是当我想在将用户重定向到另一端后立即执行它时遇到了问题。所以这是代码,它显示了我的问题。我在 html 端使用提交按钮。

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 thisquestion this should work

但是我既没有收到警报,也没有调用注销功能。基于这个问题,这应该有效

回答by NINCOMPOOP

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().

查看RequestDispatcher#forward()的 javadoc 。

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.

forward 应该在响应提交给客户端之前调用(在响应正文输出被刷新之前)。如果已提交响应,则此方法将引发 IllegalStateException。响应缓冲区中未提交的输出在转发之前自动清除。

So when you forward(), the current responseis not committed to client and hence your htmland javascriptcode 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.

所以,当你forward(),目前response没有承诺的客户端,因此您htmljavascript代码不运行。转发意味着您当前的 Servlet 没有处理request,转发的代码应该处理它,因此您看不到out.println()当前 Servlet 内部写入的任何内容作为最终响应的一部分。Tjis 正在转发请求并在服务器端本身完成,这不是由浏览器发起的重定向。要重定向,您需要使用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 代码:

<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.

此代码将仅显示警报和调用logout()函数(如果有)。并且它显示当前表单中没有任何视觉内容的 HTML 页面。