Jsp 在servlet加载时显示加载图像
我有两个jsp页面和一个servlet。当我提交page1.jsp时,servlet会做一些繁重的工作,完成工作后会重定向到page2.jsp。当servlet工作时,我想显示一个正在加载的图像 我尝试使用ajax,但它没有显示代码中的图像 页面1.jspJsp 在servlet加载时显示加载图像,jsp,servlets,Jsp,Servlets,我有两个jsp页面和一个servlet。当我提交page1.jsp时,servlet会做一些繁重的工作,完成工作后会重定向到page2.jsp。当servlet工作时,我想显示一个正在加载的图像 我尝试使用ajax,但它没有显示代码中的图像 页面1.jsp <html> <head> <script> function image(){ var xmlhttp; if (window.XMLHt
<html>
<head>
<script>
function image(){
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState === 4)
{
document.getElementById("newdiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "test.jsp", true);
xmlhttp.send();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div id="newdiv">
<form id="form1" action="loading" method="post" onsubmit="image()">
<h1>execute thread</h1>
<input id="abc" type="submit" name="submit" value="submit"/>
</form>
</div>
</body>
</html>
test.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<div id="newdiv">
<h1>Hello World!</h1>
<img id="loading" src="images/ajax_loader.gif" style="display: block; height: 50px; width: 50px;" />
</div>
</body>
</html>
请帮忙
附言:我不想使用jquery,你的解决方案就在这里
不要在page2.jsp中指定加载图像,而是在servlet本身中指定。
例如:
它不会起作用的。。。我还有一个线程在servlet中运行。。所以我不能像这样显示加载图像。。
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Ganesh
*/
@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
for(int i=0;i<=10000000;i++)
{
out.println("<img id=loading src=images/loading.gif style=display: block; height: 50px; width: 50px; >");
}
response.sendRedirect("page2.jsp");
} finally {
out.close();
}
}