Js
Primenumbers.java
Note that the servlet uses the Servletutilities.java given earlier. Also used: Primelist.java, to create a vector;primes.java of prime numbers in a background thread, to randomly generate large digits of BigInteger type, to check whether they are primes. (The code for Primelist.java and Primes.java is omitted here.) )
Package Hall;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.util.*;
public class Primenumbers extends HttpServlet {
private static vector primelistvector = new vector ();
private static int maxprimelists = 30;
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
int numprimes = Servletutilities.getintparameter (Request, "Numprimes", 50);
int numdigits = Servletutilities.getintparameter (Request, "Numdigits", 120);
Primelist primelist = findprimelist (Primelistvector, Numprimes, numdigits);
if (primelist = = null) {
Primelist = new Primelist (Numprimes, Numdigits, true);
Synchronized (primelistvector) {
if (Primelistvector.size () >= maxprimelists)
Primelistvector.removeelementat (0);
Primelistvector.addelement (primelist);
}
}
Vector currentprimes = Primelist.getprimes ();
int numcurrentprimes = Currentprimes.size ();
int numprimesremaining = (numprimes-numcurrentprimes);
Boolean Islastresult = (numprimesremaining = = 0);
if (!islastresult) {
Response.setheader ("Refresh", "5");
}
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
String title = "Some" + numdigits + "-digit Prime Numbers";
Out.println (title) + Servletutilities.headwithtitle
"<body bgcolor=\" #FDF5E6 \ ">\n" +
"
""or more digits:" + Numcurrentprimes + ". if (Islastresult)
Out.println ("<B> done searching. </B>");
Else
Out.println ("<B> still looking for" + numprimesremaining +
"More <BLINK> ... </BLINK> </B>);
Out.println ("<OL>");
for (int i=0; i<numcurrentprimes; i++) {
Out.println ("<LI>" + currentprimes.elementat (i));
}
Out.println ("</OL>");
Out.println ("</BODY> </HTML>");
}
public void DoPost (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
Check for the existence of the same type of request (completed, or being evaluated).
If present, the existing result is returned instead of starting a new background thread.
Private Primelist findprimelist (Vector primelistvector,
int Numprimes,
int numdigits) {
Synchronized (primelistvector) {
for (int i=0; i Primelist primes = (primelist) primelistvector.elementat (i);
if ((Numprimes = = Primes.numprimes ()) &&
(Numdigits = = Primes.numdigits ()))
return (primes);
}
return (NULL);
}
}
}
Primenumbers.html
! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
Large prime number calculation
to calculate several primes:
the number of digits per prime: