I created a JSP which will create a potentially infinite amount of output.
When I told the browser to stop, the browser stopped, but my console told me that the JSP's servlet kept going and going and going.
I'm wondering whether and how I could modify this code so that it will stop if the browser stops receiving data:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" errorPage="ExcessOutputErrorPage.jsp" %>
<%@ page import="de.svenjacobs.loremipsum.LoremIpsum" %>
<%@ page buffer="8kb" autoFlush="true" %>
<%!
private int dumpCount = 0;
private String nextDump()
{
dumpCount++;
String dumpHeader = "Dumping " + dumpCount + " paragraphs";
String dump = "<h2>" + dumpHeader + "</h2>";
开发者_StackOverflow中文版 LoremIpsum loremIpsum = new LoremIpsum();
System.out.println(dumpHeader);
for (int i=0; i<dumpCount; i++)
{
dump += "<p>" + loremIpsum.getParagraphs(1) + "</p>";
}
return dump;
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Large Amount of Text</title>
</head>
<body>
<h1>Large Amount of Text</h1>
<%
boolean doDump = true;
while (doDump)
{
out.println(nextDump());
out.flush();
}
%>
</body>
</html>
If you want to control the process from Browser, you may want to look at AJAX. Expose the loremIpsum object and have the AJAX code call back to your server in order to continue updating your screen.
Right now with an infinite loop running full time on your application server, multiple requests can come in and severely slow down the server.
based on what I can see of the code you posted, the loremIpsum object doesn't appear to be specific to the user, so you should be safe to use AJAX in order to move the looping and majority of the work off the server resources and into JavaScript/AJAX.
精彩评论