• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't get book example to work

 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just started to read and work through the examples in Jason Hunter's Java Servlet Programming. I can't get Example 3-6 p. 52 to work and this is driving me crazy! I modified the code a little, but it is esentially the same. The example is to demonstrate that servlets can execute between access.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PrimeSearcher extends HttpServlet implements Runnable {
int count = 0;
long lastprime = 0;
Date lastprimeModified = new Date();
Thread searcher;

public void init() throws ServletException {
searcher = new Thread(this);
searcher.setPriority(Thread.MIN_PRIORITY);
searcher.start();
}

public void run() {
long candidate = 11L;
while(count < 1000) {
if(isPrime(candidate)) {
count++;
lastprime = candidate;
lastprimeModified = new Date();
}
candidate += 2;

try {
searcher.sleep(200);
}
catch (InterruptedException ignored) { }
}
}

private static boolean isPrime(long candidate) {
long sqrt = (long)Math.sqrt(candidate);
for(long i = 3; i<= sqrt; i+=2) {
if(candidate % i == 0) return false;
}
return true;
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
if(lastprime == 0) {
out.println("Still searching for first prime...");
} else {
out.println("The last prime discovered is " + lastprime);
out.println(" at " + lastprimeModified);
}
}

public void destroy() {
// searcher.stop();
}
}
This should find primes right away and accessing the servlet quickly for the second time I should find that a prime has been found. All I get is "still searching". At this point I can't tell what is wrong. Any help would be appreciated. Thanks.
 
BJ Grau
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess, but try not changing the priority of the Thread searcher, or if you do want to set it, set it to Thread.NORM_PRIORITY
[ April 24, 2002: Message edited by: BJ Grau ]
 
Mike Dunn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you get the code from the book to work? Exactly what changes did you make?
Mike
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BJ Grau, I tried your suggestion of changing the thread priority - no luck. I tried with both the original and the changed version. I list the changes made from the original below. Basically, the changes are to encounter the primes faster, so I do not have to wait as long! The original was looking for primes after one quadrillion. If anyone gets the code to work as written, please respond, it probably means that I am not doing something right. I might add that this is with Tomcat 4.0, thanks.
 
Mike Dunn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
I just got your example code working as is with tomcat-3.3a. I compiled it with jdk1.3.1_03. I didn't do anything special. I just copied the class file to the webapps/examples/WEB-INF/classes directory and invoked it with the following URL:
http://localhost:8080/examples/servlet/PrimeSearcher
I'm up to 2447 now! I would give tomcat-3.3a a try.
Mike
 
Karl Moss
Author
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took the code listed above and was able to run it as-is with Tomcat 4.0. I compiled and placed the resulting class in webapps/ROOT/WEB-INF/classes and invoked via:
http://localhost:8080/servlet/PrimeSearcher
With each refresh it shows a new prime number (as expected).
Are you deploying differently? My Tomcat configuration should be the default (I haven't made any changes that I remember).
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for running the code for me. I did not think that there should be anything wrong with the code. However, maybe something with Tomcat is not setup correctly. I have sucessfully run all other examples up to this point.
A change that I made was to configure Tomcat to run on the default HTTP port (80) by changing the port from 8080 to 80. This is to keep from having to type http://localhost:8080/...... and instead just type http://localhost/servlet/PrimeSearcher but this should not be the problem.
The class files are in E:\jakarta-tomcat-4.0.1\webapps\ROOT\WEB-INF\classes I do not know what I am doing different. Maybe, just maybe I can have a small stroke of brilliance, now that I know that it works both on Tomcat 3.3 and 4.0 and figure out what I am doing wrong. But once again thanks for running it.
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep code works fine - I followed the same steps as per Karl
 
BJ Grau
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So everything appears to be in place for the example to work for you. Everybody has tried it on different versions of Tomcat (I used 3.2.3) and it has worked for them. Since you are seeing the message that it is still searching for the first prime, we can assume that Tomcat is working for you.
Did you try changing the Thread priority?
How long did you let it run before accessing the page a second time?
Are there any messages in the Tomcat log?
Will Tomcat handle more requests for other servlets or JSPs after you run this?
What platform are you running on?
[ April 25, 2002: Message edited by: BJ Grau ]
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This turned out to be a browser caching issue. Thanks for the support. Here is how your fellow ranchers figured it out. web page
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!