Servlet Basic Structure
Lifecycle
IMPORTANT: each time the same servlet
is invoked, ONLY a new thread is created, NOT a new independent
process.
|
First Time Servlet Invoked:
- Start the OS process of the Servlet running on the server
- Call the init method
|
Nth Time Servlet Invoked (N>1):
- A new thread associated with this invokation is created.
- Call service method
|
Servlet Instance is going to be illiminated by server:
- Call destroy method associated with this instance (thread).
|
Standard Methods of the HttpServlet class |
init()
init(ServletConfig c)
- You will have only one of the init methods implemented in
your servlet clas
- Invoked only the first time that the servlet is invoked.
- Used like init() method of applet to initialize parameters,
possilby setup persistent connections, perform one-time compuations,
etc.
- Version of init with ServletConfig parameter used to
read server-specific parameters.
- Server-Specific parameters stored in a pre-specified file
(see documentation for your server).
- example of
a server file containing such parameters or inside of
web.xml file
- example code
of a simple servlet using the file above.
- location of such a file is dependent on the server configuration,
see documentation
- Use the getInitParameter method to retrieve a parameter
value from this file. See API, use like getParameter
method of Applet class.
- generally do only for a minimal number of parameters as
using by default a shared server file with other programmers.
- If do not like the concept of editing
this server-side file (which by default is one file shared with
everyone), can simple perform File I/O with a file located anywhere
on the server your servlet would have access to.
- NOTE: if you are implementing
the init with the ServletConfig parameter, you MUST make
the call super.init(config) as the first line of your method!!!
This is because the ServletConfig object is used elsewhere in
the servlet and the default (parent) method will register it for
use.
|
service(HttpServletRequest req, HttpServletResponse res)
- This method is called whenever the servlet is invoked, the
first or Nth time.
- It is responsible for checking the HTTP request type (GET,POST,
PUT, DELETE, OPTIONS, TRACE, etc.) and calling the appropriate
servlet method doGet, doPost, doPut, doDelete (see below).
- TIP: In general you do not want
to override the service method....instead implement the methods
it will call (doGet, etc). Otherwise, you must do this HTTP
request type parsing yourself.
- WARNING: as you can have multiple
requests (simultaneous or near) for a servlet, note that you can
have the default service method calling the appropriate doGet,
etc. methods and these can execute at the same time. Hence
in ALL the doXXX methods you MUST be careful to synchronize access
to shared data!!!!!
- To allow only one servlet instance
(thread) to run at a time have your servlet implement the SingleThreadModel
interface. This queues up servlet requests and allows
only one to run at a time. BUT, YOU STILL MUST synchronize
access to to shared data, as threads can swap running.
|
doGet(HttpServletRequest req, HttpServletResponse res)
- Called by default service method to handle HTTP GET requests
made by user.
- GET requests generated by: user typing in a URL on address
line of browser, following a link from a Web-page, or submits
an HTML form that either specifies GET as its METHOD or does
not specify a MEHTOD type.
- System uses status line and header settings of doGet to
answer HEAD (HTTP type) requests. System returns whatever
headers and status code doGet sets, but omits the web-page body.
|
doPost(HttpServletRequest req, HttpServletResponse res)
- Called by default service method to handle HTTP POST requests
made by user.
- POST requests generated by: user submits an HTML
form that specifies POST as its METHOD
|
doDelete
- Called by default service method to handle HTTP DELETE requests
made by user.
|
doOptions
- Called by default service method to handle HTTP OPTIONS
requests made by user.
|
doTrace
- Called by default service method to handle HTTP TRACE requests
made by user.
- TRACE is an HTTP request type used for client debugging.
It returns the HTTP request headers back to the client
|
destroy
- Whenever the server decides to remove a servlet instance
(because requested to do so, or servlet is idle for a long time),
this method is called.
- Allows you to clean up transactions, close-down connections,
etc. Like the destroy method for the Applet class.
|
getServletContext
- returns a ServletContext object
that can be used to store persistent data and this is shared by
ALL servlets currently running in the server's servlet engine
(this means not just your servlet or its many instances but, any
servlet in the same engine can have access to this info)
- ServletContent has the following useful methods:
- setAttribute = lets you set an attribute with a
value shared by all Servlets.
- getAttribute = lets you retrieve an attribute shared
by all Servlets
|
|