Servlets

Un article de Sometimes Kitties Think Too.

+ there are only single instances of the servlet class

+ each request spawns a unique thread/stack

+ idemptotent requests (GET, PUT, HEAD)

+ HTTP 1.1 says POST is non-idempotent

+ some getRequestParameteres:

    getServerPort - to which port was the original request sent?
    getLocalPort - on which port did the request finally end up?  (each request ends up on a different port ???)

+ HttpServletRequest extends ServletRequest adding various methods like: getMethod(), getCookies(), getHeader()

+ ServletContextListener

    notified when context is initialized and destroyed
    public void contextInitialized(ServletContextEvent event) 

    public void contextDestroyed(ServletContextEvent event)


Servlet Listeners

Listener InterfaceEvent type
ServletContextAttributeListener ServletContextAttributeEvent
HttpSessionListener HttpSessionEvent
ServletRequestListener ServletRequestEvent
ServletRequestAttributeListener ServletRequestAttributeEvent
HttpSessionBindingListener HttpSessionBindingEvent valueBound
valueUnbound
HttpSessionAttributeListener HttpSessionBindingEvent attributeAdded
attributeRemoved
attributeReplaces
ServletContextListener ServletContextEvent contextInitialized
contextDestroyed
HttpSessionActivationListener HttpsSessionEvent sessionDidActivate
sessionWillPassivate


+ context scope isn't thread-safe

  (because more than one servlet can access the ServletContext)
  The key is to lock the ServletContext object
  e.g.
    synchronized {
       this.getServletContext.setAttribute("something", "else");
    }


+ session scope is also not thread-safe

 (client could open multiple browsers and thereby make multiple requests)
 
  solution is to synchronize on the HttpSesson

+ request and local variables are thread safe


+ the DD (deployment descriptor) determines the order of the registered listeners!!

+ request.getQueryString() is also a valid method

+ the SingleThreadModel creates an instance for each request

+ <load-on-startup> determines the order of initialization