You are on page 1of 35

Servlets:

The Servlet Life Cycle

Ethan Cerami
New York University

10/17/08 Servlet Life Cycle 1


Road Map
 Overview of the Life Cycle
 Birth of a Servlet
 Life of a Servlet
 Threading Issues
 Death of a Servlet
 Tips for Debugging Servlets

10/17/08 Servlet Life Cycle 2


Overview of Servlet Life Cycle

10/17/08 Servlet Life Cycle 3


Life of a Servlet
 Birth: Create and initialize the servlet
 Important method: init()
 Life: Handle 0 or more client requests
 Important methods: service(), doGet(), and
doPost().
 Death: Destroy the servlet
 Important method: destroy()

10/17/08 Servlet Life Cycle 4


Birth of a Servlet

10/17/08 Servlet Life Cycle 5


The init() method
 The init() method is called when the
servlet is first requested by a browser
request.
 It is not called again for each request.
 Used for one-time initialization.

10/17/08 Servlet Life Cycle 6


Simple Example
 The init() method is a good place to
put any initialization variables.
 For example, the following servlet
records its Birth Date/time…

10/17/08 Servlet Life Cycle 7


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

public class Birth extends HttpServlet {


Date birthDate;

// Init() is called first


public void init() throws ServletException {
birthDate = new Date();
}
10/17/08 Servlet Life Cycle 8
// Handle an HTTP GET Request
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println ("I was born on: "+birthDate);
out.close();
}
}

10/17/08 Servlet Life Cycle 9


Another Example
 This servlet initializes a set of Lottery
Numbers…
 Listing 3.7 in Text:
LotteryNumbers.java

10/17/08 Servlet Life Cycle 10


Life of a Servlet

10/17/08 Servlet Life Cycle 11


Life of a Servlet
 The first time a servlet is called, the
Servlet is instantiated, and its init()
method is called.
 Only one instance of the servlet is
instantiated.
 This one instance handles all browser
requests.

10/17/08 Servlet Life Cycle 12


Service() Method
 Each time the server receives a request for a servlet,
the server spawns a new thread and calls the
servlet’s service () method.

Browser service()
service() Single Instance
Browser Web Server
service() of Servlet

Browser

10/17/08 Servlet Life Cycle 13


Let’s Prove it…
 To prove that only one instance of a servlet is
created, let’s create a simple example.
 The Counter Servlet keeps track of the number of
times it has been accessed.
 This example maintains a single instance variable,
called count.
 Each time the servlet is called, the count variable is
incremented.
 If the Server created a new instance of the Servlet
for each request, count would always be 0!

10/17/08 Servlet Life Cycle 14


import java.io.*;
import javax.servlet.*; Only one instance of the
import javax.servlet.http.*; counter Servlet is created.
Each browser request is
public class Counter extends HttpServlet {
therefore incrementing the
// Create an instance variable
int count = 0; same count variable.

// Handle an HTTP GET Request


public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
count++;
out.println ("Since loading, this servlet has "
+ "been accessed "+ count + "
times.");
out.close();
}
}10/17/08 Servlet Life Cycle 15
The Service Method
 By default the service() method checks the
HTTP Header.
 Based on the header, service calls either
doPost() or doGet().
 doPost and doGet is where you put the
majority of your code.
 If your servlets needs to handle both get and
post identically, have your doPost() method
call doGet() or vice versa.

10/17/08 Servlet Life Cycle 16


Thread Synchronization
 By default, multiple threads are accessing the
same servlet object at the same time.
 You therefore need to be careful to
synchronize access to shared data.
 For example, the code on the next slide has a
problem…

10/17/08 Servlet Life Cycle 17


package coreservlets;
This code is problematic.
Can result in a race condition,
import java.io.*; where two users can actually
import javax.servlet.*; get the same User-ID!
import javax.servlet.http.*;
For example:
public class UserIDs extends HttpServlet {
private int nextID = 0; User 1 makes request:

String id = "User-ID-" + nextID;


public void doGet( Gets nextId of 45.
HttpServletRequest request,
HttpServletResponse response) Now User 2 makes request,
throws ServletException, IOException {and pre-empts user 1:
response.setContentType("text/html");
PrintWriter out = response.getWriter(); String id = "User-ID-" + nextID;
String title = "Your ID"; Gets nextId of 45 (same one!)
String docType = …
Admittedly, this case is rare,
String id = "User-ID-" + nextID; but it’s especially problematic.
out.println("<H2>" + id + "</H2>"); Imagine if user Id was tied to
nextID = nextID + 1; credit card number!
out.println("</BODY></HTML>");
}
}10/17/08 Servlet Life Cycle 18
How to Solve Synchronization Problems
 You have a few options for solving servlet
synchronization issues:
1) Never use instance variables in your servlets. If
you don’t have shared instance variables, you
don’t have shared synchronization problems.
2) Synchronize code explicitly with Java
synchronization blocks.
3) Use the SingleThreadInterface (not
recommended)

10/17/08 Servlet Life Cycle 19


Java Synchronization
 Use a synchronization block whenever
accessing/modifying a shared variable.
 For example:

synchronized (this) {
String id = "User-ID-" + nextID;
out.println("<H2>" + id + "</H2>");
nextID = nextID + 1;
}

10/17/08 Servlet Life Cycle 20


SingleThreadModel Interface
 To prevent multi-threaded access, you can have your servlet
implement the SingleThreadModel:
public class YourServlet extends HttpServlet
implements
SingleThreadModel {

}
 This will guarantee that your servlet will only process one
browser request at a time.
 It therefore addresses most synchronization issues.
 Unfortunately, however, it can result in severe slowing of
performance, and most people strongly recommend against
using it.
 In fact, the SingleThreadModel interface is now deprecated in
the Servlet 2.4 API.
10/17/08 Servlet Life Cycle 21
Death of a Servlet

10/17/08 Servlet Life Cycle 22


Death of a Servlet
 Before a server shuts down, it will call
the servlet’s destroy() method.
 You can handle any servlet clean up
here. For example:
 Updating log files.
 Closing database connections.
 Closing any socket connections.

10/17/08 Servlet Life Cycle 23


Example: Death.java
 This next example illustrates the use of
the destroy() method.
 While alive, the servlet will say “I am
alive!”.
 When the server is stopped, the
destroy() method is called, and the
servlet records its time of death in a
“rip.txt” text file.

10/17/08 Servlet Life Cycle 24


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

public class Death extends HttpServlet {

// Handle an HTTP GET Request


public void doGet(HttpServletRequest request,
HttpServletResponse response) throws
IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println ("I am alive!");
out.close(); Continued….
}
10/17/08 Servlet Life Cycle 25
/ This method is called when one stops
/ the Java Web Server
public void destroy() {
try {
FileWriter fileWriter = new FileWriter ("rip.txt");
Date now = new Date();
String rip = "I was destroyed at: "+now.toString(
fileWriter.write (rip);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

10/17/08 Servlet Life Cycle 26


Example rip.txt file
I was destroyed at: Thu Jan 12 11:10:58 CDT 2004

10/17/08 Servlet Life Cycle 27


Putting it all together

10/17/08 Servlet Life Cycle 28


A Persistent Counter
 Now that we know all about the birth, life and
death of a servlet, let’s put this knowledge
together to create a persistent counter.
 The Counter.java example we covered earlier
has a big problem:
 When you restart the web server, counting starts
all over at 0.
 It does not retain any persistent memory.

10/17/08 Servlet Life Cycle 29


Persistent Counter
 To create a persistent record, we can
store the count value within a
“counter.txt” file.
 init(): Upon start-up, read in the current
counter value from counter.txt.
 destroy(): Upon destruction, write out the
new counter value to counter.txt

10/17/08 Servlet Life Cycle 30


port java.io.*;
port java.util.*;
port javax.servlet.*;
port javax.servlet.http.*; At Start-up,
load the
blic class CounterPersist extends HttpServlet {
counter from
tring fileName = "counter.txt";
file.
nt count;
In the event of
ublic void init () { any exception,
try { initialize
count to 0.
FileReader fileReader = new FileReader (fileName);
BufferedReader bufferedReader = new BufferedReader (fileR
String initial = bufferedReader.readLine();
count = Integer.parseInt (initial);
} catch (FileNotFoundException e) { count = 0; }
catch (IOException e) { count = 0; }
catch (NumberFormatException e) { count = 0; }
Continued….
10/17/08 Servlet Life Cycle 31
andle an HTTP GET Request
ic void doGet(HttpServletRequest request, HttpServletResponse re
hrows IOException, ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
count++;
out.println ("Since loading, this servlet has "
+"been accessed "+ count + " times.");
out.close();
Each time the
doGet()
method is
called,
increment the
countContinued….
variable.
10/17/08 Servlet Life Cycle 32
// At Shutdown, store counter back to file
public void destroy() {
try {
FileWriter fileWriter = new FileWriter (fileName);
String countStr = Integer.toString (count);
fileWriter.write (countStr);
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
} When destroy()
} is called, store
} Any problems new counter
with this code? variable back
to counter.txt.

10/17/08 Servlet Life Cycle 33


Tips for Debugging Servlets
 Section 3.8 of our Text has a few tips for
Servlet Debugging. Pay close attention!
 At this stage, the best debugging option is to
use print statements.
 print statements will be output to the Tomcat
console (very useful!)
 Let’s try it out…
 For production systems, a real logging library,
such as Log4J, is a much better option.

10/17/08 Servlet Life Cycle 34


Summary
 Servlets have three distinct phases:
 birth: refer to the init() method
 life: refer to the service() method
 death: refer to the destroy() method
 The first time a servlet is called, the Servlet is
instantiated, and its init() method is called.
 Only one instance of the servlet is ever
instantiated.
 This one instance handles all browser
requests (don’t ever forget this!)

10/17/08 Servlet Life Cycle 35

You might also like