views:

62

answers:

1

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>";
        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"&gt;
<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>
+1  A: 

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.

Sean
Interesting idea, but the loop isn't actually supposed to be infinite -- I expected it to stop when the browser disconnected. This doesn't however seem to be the case and I'm trying to figure out how to stop it. I suppose I could put some JavaScript into the webpage telling it to phone home when the user leaves the page, but that would rely on the browser having JavaScript enabled.
Brian Kessler