Servlet Life cycle - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Servlet Life cycle

Description:

Then the service method is called ... variables to deal with any concurrency issue ... public class MyServlet extends HttpServlet. private int visitorCount; ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 9
Provided by: julie387
Category:
Tags: cycle | life | method | servlet

less

Transcript and Presenter's Notes

Title: Servlet Life cycle


1
Servlet Life cycle
  • If a client request for a servlet is the first
    request and the servlet is not loaded, then the
    init method is called otherwise, the init method
    is not called
  • Then the service method is called
  • The service method automatically calls doGet,
    doPost, or another HTTP request method
  • The server automatically calls the destroy method
    before shutting down the servlet

2
Servlet init method
  • Use the init method to initialize instance
    variables, or perform other initialization tasks,
    in particular some that might be cpu intensive
    such as accessing files,

3
Servlet threads
  • Each client request to a servlet generates a new
    thread ? multithreaded environment
  • ? potential concurrency issues when accessing
    common resources
  • Instance variables of the servlet
  • Files
  • Databases

4
Instance variables of a Servlet
  • A servlet can have instance variables, for
    instance a variable keeping track of the visitor
    count
  • Since we can have multiple threads of a single
    servlet running concurrently, any instance
    variable can be accessed simultaneously by
    several threads
  • ? Concurrency is an issue
  • We can synchronize access to instance variables
    to deal with any concurrency issue

5
Instance variables of a Servlet
  • public class MyServlet extends HttpServlet
  • private int visitorCount
  • / initialize the instance variables inside the
    init
  • method /
  • public void init( ) throws ServletException
  • visitorCount 0
  • // code rest of the class here

6
Instance variables of a Servlet
  • // code rest of the class here
  • public void doGet( HttpServletRequest request,

  • HttpServletResponse response)
  • throws IOException,
    ServletException
  • synchronized( this )
  • // only 1 thread at a time will access
    this block
  • visitorCount
  • // display the visitor count

7
Instance variables of a Servlet
  • public void doGet( HttpServletRequest request,

  • HttpServletResponse response)
  • throws IOException,
    ServletException
  • int localVisitorCount 0
  • synchronized( this )
  • // only 1 thread at a time will access
    this block
  • visitorCount
  • localVisitorCount visitorCount
  • // Process localVisitorCount here

8
Concurrency issues - Servlet
  • You can also resolve concurrency issue by coding
    synchronized methods
  • For instance access to a file or a database can
    be done via a synchronized method
  • public synchronized void foo( )
  • // thread-safe code here
Write a Comment
User Comments (0)
About PowerShow.com