English 中文(简体)
如果浏览器已经停止,我如何停止此JSP?
原标题:How can I stop this JSP if the browser has stopped?

我创建了一个JSP,它将创建潜在的无限量的输出。

当我告诉浏览器停止时,浏览器停止了,但控制台告诉我JSP的servlet一直在运行。

我想知道是否以及如何修改此代码,以便在浏览器停止接收数据时停止:

<%@ 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">
<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>
问题回答

如果你想从浏览器控制这个过程,你可能想看看AJAX。暴露loremIpsum对象,并让AJAX代码回调到您的服务器,以便继续更新您的屏幕。

现在,由于应用程序服务器上有一个无限循环全职运行,多个请求可能会进入并严重降低服务器的速度。

根据我所看到的你发布的代码,loremIpsum对象似乎不是特定于用户的,所以你应该安全地使用AJAX,以便将循环和大部分工作从服务器资源转移到JavaScript/AAJAX中。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签