• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can servlets use Threads

 
Senthil Somasundaram
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am bit new to Servlets. I attended a test on servlets and Jsp.Pls give me the answer for this
questions,
1. can we use threads in java
2.Can we initialize servlet using constructor than init() method
3.can we call destroy method from servlet
4If System.exit() can used in servlets

5. Can we call another servlet method from existing one.
6.What will be the method(GET or POST or depend upon prev Req) followed when servlet uses RequestDispatcher

7.Can we create mor than one instance of servlets.
Thank You,
Senthil
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Sure - but not recommended for beginner
2. NO! - constructors are handled by the servlet engine automatically.
3. NO! - another task handled by the servlet engine.
4. Only if you want to kill the server entirely - may hit a security exception instead.
5. Not a good idea, put common code in a helper class.
6. As I recall, you get the original method because it is already parsed out in the request.
7. Normally the servlet engine creates only one instance.
Bill
[ July 10, 2003: Message edited by: William Brogden ]
 
John Haake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to address #1. In all others I'd give the same reply as Bill already did.
We have a very successful production system that brings UDP messages coming in from our stores into our Enterprise Applications at the home office. We did this using a Servlet (set to run at startup) that starts one Manager thread that then "manages" many other threads for the reading of these UDP messages and passing them on to specific EJBs. The Servlet also doubles as our status monitor for all the threads an the work they've performed. Servlet threads have certainly worked for us ... but with all threading projects care must be taken.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good example John.
Another example might be where you want to send emails from a servlet - sticking the mail server communication in its own Thread makes things alot easier.
Bill
 
Senthil Somasundaram
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John & bill.

Sen
 
John Haake
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the safety code in our servlet for starting the manager thread to insure we only get one Manager started.
private void startupUdpMessaging() {
doLog( "UdpMessaging Startup", null );
if ( manager == null ) {
synchronized( lockManager ) {
if ( manager == null ) {
try {
doLog( "Manager starting (manager == null)", null );
manager = new Manager();
manager.setRunning( true );
} catch( Exception e ) {
doLog( "Exception on manager initialization", e );
}
}
}
} else if ( ! manager.isRunning() ) {
synchronized( lockManager ) {
if ( ! manager.isRunning() ) {
try {
doLog( "Manager re-starting", null );
manager.setRunning( true );
} catch( Exception e ) {
doLog( "Exception on manager initialization", e );
}
}
}
} else {
doLog( "UdpMessaging system already running", null );
}
}
private void shutdownUdpMessaging() {
doLog( "UdpMessaging Shutdown", null );
if ( manager != null ) {
synchronized( lockManager ) {
if ( manager != null ) {
doLog( "setting manager's running flag = false", null );
manager.setRunning( false );
}
}
} else {
doLog( "UdpMessaging system already shutdown", null );
}
}

I hope that helps, syncronization is important.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic