Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
7.3 Persistent Servlet State and Auto Reloading Pages
155
apply to servlets. Servlets can also store persistent data in the 
Servlet 
Context
 object that is available through the 
getServletContext
 method.
ServletContext
 has 
setAttribute
 and 
getAttribute
 methods that let
you store arbitrary data associated with specified keys. The difference
between storing data in instance variables and storing it in the 
Servlet 
Context
 is that the 
ServletContext
 is shared by all servlets in the servlet
engine (or in the Web application, if your server supports such a capability).
Listing 7.1 shows the main servlet class. First, it receives a request that
specifies two parameters: 
numPrimes
 and 
numDigits
. These values are nor 
mally collected from the user and sent to the servlet by means of a simple
HTML form. Listing 7.2 shows the source code and Figure 7 1 shows the
result. Next, these parameters are converted to integers by means of a simple
utility that uses 
Integer.parseInt
 (see Listing 7.5). These values are then
matched by the 
findPrimeList
  method  to  a 
Vector
 of recent or ongoing
calculations to see if there is a previous computation corresponding to the
same two values. If so, that previous value (of type 
PrimeList
) is used; oth 
erwise, a new 
PrimeList
 is created and stored in the ongoing calculations
Vector
, potentially displacing the oldest previous list. Next, that 
PrimeList
is checked to determine if it has finished finding all of its primes. If not, the
client is sent a 
Refresh
  header  to  tell  it  to  come  back  in  five  seconds  for
updated results. Either way, a bulleted list of the current values is returned to
the client. 
Listings 7.3 (
PrimeList.java
) and 7.4 (
Primes.java
) present auxiliary
code used by the servlet. 
PrimeList.java
 handles the background thread
for the creation of a list of primes for a specific set of values. 
Primes.java
contains the low level algorithms for choosing a random number of a speci 
fied length and then finding a prime at or above that value. It uses built in
methods in the 
BigInteger
 class; the algorithm for determining if the num 
ber is prime is a probabilistic one and thus has a chance of being mistaken.
However, the probability of being wrong can be specified, and I use an error
value of 100. Assuming that the algorithm used in most Java implementations
is the Miller Rabin test, the likelihood of falsely reporting a composite num 
ber as prime is provably less than 2
100
. This is almost certainly smaller than
the likelihood of a hardware error or random radiation causing an incorrect
response in a deterministic algorithm, and thus the algorithm can be consid 
ered deterministic. 
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved