Lifecycle of Servlet Thread....


Web tier: servlets, JSP, Web frameworks: Lifecycle of Servlet Thread....

  1. Lifecycle of Servlet Thread.... (4 messages)

    Does any one know what happens to the Servlet Thread(I mean the Thread executing for one user for the Servlet) after it returns .

    essentially I am trying to do somethinglike this...

     * Created on June 2, 2001, 12:33 PM

    package com.nj.util;

    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class AsynServlet extends javax.servlet.http.HttpServlet implements Runnable {

       private static int _idGen = 0;
       private Hashtable _readerMap = null ;
       private ObjectFIFO _queue = null;
       private Thread _reader = null;
        public void init(javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException {
           this._queue = new ObjectFIFO(100);
           this._readerMap = new Hashtable();
           this._reader = new Thread(this);
        public void service(javax.servlet.ServletRequest servletRequest,javax.servlet.ServletResponse servletResponse) throws javax.servlet.ServletException, {
           int i = getID();
           MessageContainer con = new MessageContainer(i);
           this._readerMap.put( new Integer (i) ,servletResponse);
           }catch(java.lang.InterruptedException ie)
        public static int getID()
         return _idGen ++;
        public void run() {
            MessageContainer mc = null;
            mc = (MessageContainer)this._queue.remove();
           }catch(java.lang.InterruptedException ex)
           ServletResponse response = (ServletResponse)this._readerMap.get(new Integer(mc.getId()));
           String str = mc.getResponse().toString();
           try {
              PrintWriter pw = response.getWriter();

    } catch (IOException e) {
                   System.out.println(" ERROR " + e.getMessage());

    This Async Servlet does not seem to be working ... What happens to the ServletResponse when the Thread is completed with its execution ..

    Help Appreciated...


  2. I forgot to mention ... Esseentially I am trying to make an Asynchronous Servlet ... Does anyone have a better idea

    And Also the ObjectFIFO is a FIFO Queue and there is a timelag (some value ) between a add and remove to queue

  3. Ok... first off... you can trigger asynchonous operations with a http servlet... but the servlet itself is synchronous...this is becaus http is a request-response protocol. There are several alternatives to what you are doing.... have a separate thread handle the asynchronous behaviour.... if this process is context sensetive, geta handle and store it in the session. If this thread needs to be share register it with a directory / naming service

    Another approch is to use async. messaging.... drop messages to a topic, another process picks the messages up from the topic and takes care processing it

  4. Adi ,

    That is exactly what I am doing . I am having my my own Asynbc Thread... The Servlet implemts Runnable ...

    But this program does not seem to work... The Response Object is probably destroyed or is junk after the Servlet Thread returns control..

  5. I meant to say have a diiferent class for the thread...
    Also the user will not get the response you are trying to write out, since the servlet container will close the i/o stream to the servlet service() has finished execution.

    another thing.... don't use the service method, use doGet or doPost...just call one from the other....
    doPost(req, res);
      //your code

    the continer puts initilization and cleanup code in service to manage resources.

    I donot see the point of what you are trying to accomplish here...... Async messaging on a Req-Resp wire protocol(http)!!