0

I Have a Servlet to Show up a Login Page and when i Enter the URL the Servlet has been specified by a separate Thread and do the work.But Even After Committing the Response (i.e) After Finishing the Job the Thread is still Alive why does it does so? The Alive Status Shows True after im getting the Response in the Page.

Code:

public class LoginNew extends HttpServlet implements Runnable {
    protected void service(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        PrintWriter writter = response.getWriter();
        writter.println(
                "<!DOCTYPE html>" + 
                "<html>" + 
                "<form>" + 
                "<div style=\"text-align:center\">UserName   :<input type=\"text\" name=\"UserName\"><br>" +
                "<div style=\"text-align:center\">Password   :<input type=\"password\" name=\"Password\"><br>" +
                "<div style=\"text-align:center\">TimeOut Interval   :<input type=\"text\" name=\"TimeOut\"><br>" +
                "<div style=\"text-align:center\">No TimeOut :<input type=\"checkbox\" name=\"NoTimeout\" value=\"1\"> NoTimeout <br><br>" +
                "<input type=\"submit\" formaction=\"Home\" formmethod=\"post\" value=\"Login\" />" + 
                " "+
                "<input type=\"submit\" formaction=\"SignUp\" formmethod=\"post\" value=\"SignUp\" />" + 
                "</form>" + 
                "</body>" + 
                "</html>"
        );
        Thread.currentThread().sleep(60000);
        System.out.println("After Commitment Thread -> "+Thread.currentThread().getName()+" Alive Status--> "+Thread.currentThread().isAlive());
    }
}
Jakob F
  • 870
  • 5
  • 19

2 Answers2

1

Based on your code, It looks like you are checking the liveness of the thread inside the thread, ofcourse it is alive in that moment. For more clarity you can use VisualVM program, it shows each generated thread of your program and other useful information about.

Isen
  • 107
  • 1
  • 7
1

There are number of misconceptions and oddities here:

  1. If you can call Thread.currentThread().isAlive(), it is always going to return true. If the current thread is not alive, it is not in a position to execute anything.

  2. A typical servlet container does not terminate request threads. That would be inefficient. Instead, when a request has finished, the request thread is returned to the pool so that it is available for the next request.

  3. "Even after committing the response; i.e. after finishing the job ...". That is not what "committing a response" means. What it actually means is that the HTTP response header has been written. That means that it is no longer possible to change the response code or update any of the response headers. (In your example, the response was committed when you called getWriter.)

  4. In fact, a response cannot actually finish until after your PrintWriter has been closed. This could be an explicit close in your service method, or the implicit close that will happen after the service call returns.

  5. In your example, you are sleeping for 60 seconds between writing the response body and returning. Since you didn't flush the Writer after the write, this will probably mean that the client (e.g. browser) has to wait 60 seconds before the complete body arrives.

  6. The sleep will also be locking up one of the request threads for 60 seconds. If too many requests do this simultaneously, you are liable to tie up all of the threads. This is a bad idea.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096