Thursday, December 27, 2012

Reading Note of Core Servlets and JavaServer Pages

How to deal with requests that take a long time to process?

7.4 Persistent Servlet State and Auto-Reloading Pages
To deal with requests that take a long time to process (or whose results periodically change), you need the following capabilities:

• A way to store data between requests.  For data that is not
specific to any one client, store it in a field (instance variable)
of the servlet. For data that is sp ecific to a user, store it in the
HttpSession  object (see Chapter 9, “Session Tracking”). For data
that needs to be available to other  servlets or JSP pages, store it in
the  ServletContext (see the section on sharing data in Chapter
14, “Using JavaBeans Components in JSP Documents”).

• A way to keep computations running after the response is sent 
to the user. This task is simple: just start a  Thread. The thread
started by the system to answer requests automatically finishes when
the response is finished, but other threads can keep running. The only
subtlety: set the thread priority to a low value so that you do not slow
down the server.

• A way to get the updated results to the browser when they are 
ready.  Unfortunately, because browsers do not maintain an open
connection to the server, there is  no easy way for the server to
proactively send the new results to the browser. Instead, the browser
needs to be told to ask for updates. That is the purpose of the
Refresh response header.


No comments:

Post a Comment