Skip to content

Servlet Basics


Concepts

  • Overview
  • Basic Servlet Structure
  • A Servlet That Generates Plain Text
  • A Servlet That Generates HTML
  • The Servlet Life Cycle
  • Servlet Debugging

Servet Overview

  1. Read the explicit data sent by the client.
    • This is what is typed into the location field in a browser, or a link that is clicked on, or an uploaded file, etc. Whatever you intend to send to the server.
  2. Read the implicit HTTP request data sent by the client.
    • The HTTP Request Header records.
  3. Generate the results.
    • The servlet does some work like talk to a database, work with the HTTP session, access collections, create JavaBeans, etc.
  4. Send the explicit data.
    • There are many ways that a web application can return something to a browser.
    • What is returned could be an HTML page but also other things like audio, video, or images.
  5. Send the implicit data.
    • The HTTP Response Headers

Basic Servlet Structure

  1. Import these packages:

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    
  2. Subclass HttpServlet

    public class TrivialServlet extends HttpServlet {
    
  3. Annotate the servlet

    @WebServlet(
        name = "trivialServlet",
        urlPatterns = { "/trivial", "/simple" }
    )
    

    Note

    The annotation above only requires one urlPattern. The use of two patterns above is shown only as an example.

  4. Implement a method with this signature:

    // The main servlet method
    public void doGet(HttpServletRequest request, HttpServletResponse response
            throws ServletException, IOException {
    
  5. The doGet() method is where all the work is done.

A Servlet That Generates Plain Text

A Servlet That Generates HTML

The Servlet Life Cycle

  • Only one instance of a servlet is loaded into the servlet engine! That one servlet instance will handle all the requests for that servlet. Even there are many simultaneous requests the one instance will handle them all.

init() method

  • Runs only once before the servlet is available to the browser.
  • Is used to initialize the servlet.
  • Is used for general initializations
  • Is used for initializations controlled by initialization parameters.
    public void init() throws ServletException {
    
        //Initialization code
    
    }
    

doGet() and doPost() methods

  • Contain the real work in a servlet.
  • Are called based on the HTTP request. If the request is a GET then doGet() is called. If it is a POST then doPost() is called.
  • Called once for every request from a browser.
  • Are handed the Request and Response objects for processing.

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
        //The real heart of java web programming here!
    
    }
    
  • Servlets run in a multi-threaded environment. (More on this coming up!)

destroy() method

  • Is used to shutdown the resources started up in the init() method.
  • Is called once when the servlet engine is shutting down.
  • Is not required and is often not used.
    public void destroy() { 
        //Code to shut things down
    }