Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Quite Basic Question

843841Mar 23 2004 — edited Mar 23 2004
I am learning Java/Servlet, in the following code, there are methods: init(), run(), doGet() ..., could you please tell me the running order? init(), run(), doGet() or init() doGet() run()?

When I ran the servlet, I guess run() method hasn't been executed.
Thx in advance.

Wolf
==========================================================
import java.io.*;
import java.util.*;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author SYSOPS
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class PrimeSearcher extends HttpServlet {

long lastprime=0;
Date lastprimeModified=new Date();
Thread searcher=null;

public void init(ServletConfig config) throws ServletException {
super.init(config);
searcher=new Thread();
searcher.setPriority(Thread.MIN_PRIORITY);
searcher.start();

//TODO Method stub generated by Lomboz
}
public void run(){
// long candidate=1000000000000001L;
long candidate=2L;
lastprime=288;

while (true){
if (isPrime(candidate)){
lastprime=candidate;
lastprimeModified=new Date();
}
candidate+=2;
try {
Thread.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 destroy() {
super.destroy();
searcher.stop();
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out=response.getWriter();
out.println("Start at candidate..."+ lastprimeModified);
try {
Thread.sleep(2000);
}
catch(InterruptedException ignored){}
if (lastprime==0){
out.println("Still searching for first prime..."+ lastprimeModified);
}
else {
out.println("The last prime discovered was "+lastprime+" at "+ lastprimeModified);
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2004
Added on Mar 23 2004
2 comments
110 views