🎮

Servlet

What?
  • a technology that includes APIs and Java classes.
    đź’ˇ
    Note: the Servlet technology is not limited to the HTTP protocol. “Servlet interface” and “GenericSevlet Class” follow the idea that a servlet could be used for any type of service (”not protocol dependent”), such as HTTP, FTP,…etc,… → 2 parameters request and respond are protocol-agnostic.

    • Are Java classes that dynamically process HTTP requests and construct responses.
  • a web component that runs on the server side (middle tier) and can act as a controller:
    • Are Java codes that are used to add dynamic content to Web server.
    • receive requests, handle and reply back with a response.
      • There is only a single instance of Servlet created on the Web server.
      • A Servlet’s initializing code is used only for initializing in the 1st time
      • To service multiple clients’ requests, the Web server creates multiple threads for the same Servlet instance (Overcome CGI’s consumed more memory)
      • Gets auto refreshed on receiving a request each time
    • Servlets are under the control of another Java application called a Servlet Container.
  • Merits
    • Enhanced efficiency (initializing only once, auto refresh)
    • Ease to use (using Java combining HTML)
    • Powerful (using Java)
    • Portable
    • Safe and cheap
  • Demerits
    • Low-level HTML documentation (Static well-formed-ness is not maintained)
    • Unclear-session management (flow of control within the codes is very unclear)
Servlet Class
  • a Java class, has only a single instance
  • must be declared in the web.xml file / by annotations
    • Servlets Declaration in web.xml and be created cell memory by container
    • Servlet Mapping to define the URL for accessing Servlet via url-pattern
    • annotations
  • Servlet URL Pattern:
Servlet Object
  • be initialized
    • by Container
    • in 3 steps:
      1. load Servlet Class
      1. Instantiation: Create Servlet Instance
      1. Initialization: call init(ServletConfig)
    • at either of the stages (depending on the custom setup):
      • app deploy
      • the first request for the servlet is received by the Web Container.
  • Exist until right before undeployed or server is crashed
  • Output HTML String into ResObj
  • must be mapped with an URL pattern for accessing.
life-cycle:
  • who: container.
  • when: at either of the stages (depending on custom setup):
    • app deploy
    • in the first request for the servlet is received by the Web Container.
  • how:
    1. load Servlet Class
    1. Instantiation: Create Servlet Instance
    1. Initialization: init(ServletConfig)
      • is called only one by the container after Servlet Obj is initialized successfully.
      • support to replace the default constructor
    1. services()
      • doHTTPMethods → process every/each request
    1. destroy() :
      • called only one right before undeployed or the web server is stopped.
    1. Garbage Collection: finalize()
  • when: undeployed or the web server is stopped/crashed.