简体   繁体   English

Servlet加载时加载屏幕

[英]Loading screen while Servlet loads

I have this JSP where I select certain parameters and hit " submit " button, after clicking "submit" I am calling a JavaScript function as below 我有这个JSP,我选择某些参数,然后点击“ 提交 ”按钮,点击后“提交”我如下调用JavaScript函数

<body>
<input type=button class="button"  id = "submit" value="Evaluate" 
onclick="JavaScript:return evaluateFunction()">
</body>

and in the evaluateFunction() I am collecting all the parameters and call a new Servlet in new popup window as below: evaluateFunction()函数evaluateFunction()我正在收集所有参数,并在新的弹出窗口中调用新的Servlet,如下所示:

<script>
function evaluateFunction(){
var win = window.open('ConfirmEvaluate?parameters,'mywindow','width=600,height=500,titlebar=no')
}
</script>

Now the issue is ConfirmEvaluate servlet takes some time to get the data from database(around 15-20 secs based on size of input) and displays the data in the forwarded JSP(say userdata.jsp ) Now I want to display a loading gif or screen in that 15-20 seconds while the Servlet loads the data from database. 现在的问题是ConfirmEvaluate servlet需要一些时间从数据库中获取数据(基于输入的大小大约需要15-20秒),并在转发的JSP中显示数据(例如userdata.jsp )。现在,我想显示一个加载的gif或Servlet从数据库加载数据的过程中,屏幕将在15-20秒内显示在屏幕上。 How can I proceed, any help would be appreciated. 我该如何进行,任何帮助将不胜感激。

I have already gone through some similar questions in SO but none of them is having a specific answer. 我已经在SO中经历了一些类似的问题,但没有一个有特定的答案。

You have to use AJAX. 您必须使用AJAX。 Servlet requests like the one in your example are synchronous. 像您的示例中那样的Servlet请求是同步的。 Which means it will wait until the processing finishes then do the next activity. 这意味着它将等到处理完成后再执行下一个活动。

With an AJAX request you can send the request and then do something else without having to wait for it to finish processing, because it is asynchronous. 使用AJAX请求,您可以发送请求,然后执行其他操作,而不必等待它完成处理,因为它是异步的。

The way i would approach this is in the following way: 我将采用以下方式进行处理:

You get the user details in ConfirmEvaluate, and redirect the user to userdata, then once the user is on the page do the AJAX request to fetch the information that takes a long time to process. 您可以在ConfirmEvaluate中获取用户详细信息,然后将用户重定向到userdata,然后,当用户在页面上时,请执行AJAX请求以获取处理时间较长的信息。 When the request is made you can show a loading icon but when you get a response from the AJAX request, you can hide this loading icon. 发出请求后,您可以显示一个加载图标,但是当收到AJAX请求的响应时,可以隐藏此加载图标。 Check out this brilliant post on how to make AJAX requests with servlets 查看关于如何使用servlet发出AJAX请求的精彩文章

I had to implement something like this recently, here is some example code: 我最近不得不实现类似的东西,这是一些示例代码:

<script>
//when page loads, the ajax request starts
$(document).ready(function() {
    $(this).scrollTop(0);
    getposts(username);
    });

//ajax request that will show and hide the loader depending on response
 var getposts = function (username) {
     var params = {
            user: username        
        };
$.get("../GetUserFeed",$.param(params),function(responseXml) {      
        $("#user-feed").append($(responseXml).find("feed").html()); // Parse XML, find <data> element and append its HTML to HTML DOM element with ID "somediv".
        $('#logo-loader').hide();
           if(isBlank(responseXml)){
            $('#logo-loader-completed').show();
            $('#logo-loader-image').hide();
        }
    });
 };

 </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM