Set up HTTP answer headers in JSP

Source: Internet
Author: User
Tags thread
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:








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.