Java Servlets
Java Servlets
Java Servlets
What is a Servlet?
•Powerful
•Efficiency
•Safety
•Integration
•Extensibility
•Inexpensive
•Secure
•Performance
Java Servlet
• Javax.servlet package can be extended for use
with any application layer protocol
– http is the most popularly used protocol
– Javax.servlet.http package is extension of the
javax.servlet package for http protocol
• The Servlet spec allows you to implement separate Java
methods implementing each HTTP method in your subclass
of HttpServlet.
– Override the doGet() and/or doPost() method to provide normal
servlet functionality.
– Override doPut() or doDelete() if you want to implement these
methods.
– There's no need to override doOptions() or doTrace().
– The superclass handles the HEAD method all on its own.
Anatomy of a Servlet
• init()
• destroy()
• service()
• doGet()
• doPost()
• Servlet API life cycle methods
– init(): called when servlet is instantiated; must
return before any other methods will be called
Instantiate servlet
Servlet
Call init ( ) method Perform
HTTP Initialization
Allocate request to thread Call service ( ) method
Request 1
HTTP
Allocate request to thread Call service ( ) method Perform Service
Request 2
Shutdown
Initiated
Block all further requests Wait
HTTP for active threads to end Perform Service
Response 1
Terminate thread pool