我有两个jsp页面和一个servlet。当我提交我的page1.jsp时,servlet做了一些繁重的工作,在完成工作后,它重定向到page2.jsp。当servlet完成它的工作时,我想显示一个加载图像。
我尝试使用ajax,但它不显示图像,这里是代码。
的Page1.jsp
<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>
请帮忙。
P.S:我不想使用jquery
答案 0 :(得分:-1)
您的解决方案就在这里
不要在page2.jsp中指定加载图像,在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();
}
}