jsp : 我可以更改 render/execution 的顺序吗
jsp : Could I change the order of render/execution
我正在研究 jsp,
我要实现的很简单。
- 用Java
做一些耗时的工作
- 在处理时,向用户显示微调器
所以我实现如下
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page isThreadSafe="true" %>
<%
boolean isFinish = false;
Thread executeThread = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 10; i++){
try{
System.out.println(i + " Thread ");
Thread.sleep(1000);
}catch(Exception ex){
System.err.println(ex);
}
}
}
});
executeThread.run();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Loading</title>
</head>
<body>
<div class="popup" style="text-align: center;">
<div class="l_img">
<img src="resources/image/spinner/loading_gif.gif" alt="LOADING...">
</div>
<div class="b_txt">Processing</div>
<div class="s_txt">Please, wait..</div>
</div>
</body>
<%
if(isFinish){
%>
<script>
alert('done');
</script>
<%
}
%>
</html>
但是,页面会在 Thread 终止后显示。我可以颠倒顺序吗?
我想要的顺序
- 显示html页(
spinner gif
)
- 执行Java逻辑
- 捕获Java执行的return
- 如果 return 是
true
,则用 javascript 提醒
可能吗?
感谢您的回答
您无法通过服务器端操作动态更改页面呈现。
页面将始终在 if(isFinish)
已执行的位置呈现。
只有当所有的java都执行完并且在一个块中时,服务器才会将完整的html页面发送给用户!你需要在前端实现它,例如你发送一个开始加载的基本页面,然后你用 ajax 调用或类似的东西调用长期过程。
<!DOCTYPE html>
<html>
<body onload="alert('done')">
Put your spinner here.
Some text here is necessary here for the browser to start displaying.
Otherwise it will wait until it receives enough of a response to begin displaying anything.
I am not sure how much is necessary. This works for me.
<%
out.print("<br/>Starting processing now. <br/>");
out.flush();
Thread.sleep(2000);
out.print("<br/>Processing..... <br/>");
out.flush();
for(int x = 1; x < 4; x++){
Thread.sleep(2000);
out.print("Result " + x + ".<br/>");
out.flush();
}
%>
</body>
</html>
我正在研究 jsp, 我要实现的很简单。
- 用Java 做一些耗时的工作
- 在处理时,向用户显示微调器
所以我实现如下
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page isThreadSafe="true" %>
<%
boolean isFinish = false;
Thread executeThread = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 10; i++){
try{
System.out.println(i + " Thread ");
Thread.sleep(1000);
}catch(Exception ex){
System.err.println(ex);
}
}
}
});
executeThread.run();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Loading</title>
</head>
<body>
<div class="popup" style="text-align: center;">
<div class="l_img">
<img src="resources/image/spinner/loading_gif.gif" alt="LOADING...">
</div>
<div class="b_txt">Processing</div>
<div class="s_txt">Please, wait..</div>
</div>
</body>
<%
if(isFinish){
%>
<script>
alert('done');
</script>
<%
}
%>
</html>
但是,页面会在 Thread 终止后显示。我可以颠倒顺序吗? 我想要的顺序
- 显示html页(
spinner gif
) - 执行Java逻辑
- 捕获Java执行的return
- 如果 return 是
true
,则用 javascript 提醒
可能吗? 感谢您的回答
您无法通过服务器端操作动态更改页面呈现。
页面将始终在 if(isFinish)
已执行的位置呈现。
只有当所有的java都执行完并且在一个块中时,服务器才会将完整的html页面发送给用户!你需要在前端实现它,例如你发送一个开始加载的基本页面,然后你用 ajax 调用或类似的东西调用长期过程。
<!DOCTYPE html>
<html>
<body onload="alert('done')">
Put your spinner here.
Some text here is necessary here for the browser to start displaying.
Otherwise it will wait until it receives enough of a response to begin displaying anything.
I am not sure how much is necessary. This works for me.
<%
out.print("<br/>Starting processing now. <br/>");
out.flush();
Thread.sleep(2000);
out.print("<br/>Processing..... <br/>");
out.flush();
for(int x = 1; x < 4; x++){
Thread.sleep(2000);
out.print("Result " + x + ".<br/>");
out.flush();
}
%>
</body>
</html>