i-Lab Guide To: Servlets

URL Mapping

http://www.hostname.com/contextPath/servletPath;cookieID/pathInfo?param1=value1&param2=value2

contextPath = web-app name
servletPath = servlet URL match (e.g. servlet name)
pathInfo = remainder of URI

Servlet methods (javax.servlet)

init(ServletConfig)
service()
destroy()
getServletConfig()
getServletInfo()

HttpServlet methods (javax.servlet.http)

init()
doGet(request, response)
doPost(request, response)
doPut()
doDelete()
doHead()
doOptions()
doTrace()

HttpServletRequest methods

getRequestURI()
getContextPath()
getServletPath()
getPathInfo()
getRemoteAddr()

getHeader(parameter)
getHeaders(parameter)
getHeaderNames()

getParameter(parameter)
getParameterValues(parameter)
getParameterNames()
getParameterMap()

setAttribute(name, valueObject)
getAttribute(name)
getAttributeNames()

getRequestDespatcher(path)
getRemoteUser()
isUserInRole(rolename)
getUserPrincipal()

HttpServletResponse methods

setContentType()
setHeader() setStatus() sendError(code, message) sendRedirect() addCookie() getWriter() getOutputStream()
encodeURL()

ServletConfig methods 

getServletName()
getInitParameter(name)
getInitParameterNames()
getServletContext() 

ServletContext methods

getResource(path)
getResourceAsStream(path)
setAttribute(name, valueObject)
getAttribute(name)
getAttributeNames()
getRequestDespatcher(path)

Listeners

ServletContextListener          - notification of when a context is initialised/destroyed (deployment descriptor)
ServletContextAttributeListener - notification of addition, removal, replacement of attributes (deployment descriptor)

HttpSession methods

setAttribute(name, valueObject)
getAttribute(name)
getAttributeNames()
invalidate()
setMaxInactiveInterval(secs)
getMaxInactiveInterval()

Listeners

HttpSessionAttributeListener  - notification of addition, removal, replacement of attributes (deployment descriptor)
HttpSessionBindingListener    - notification to an object when it is added/removed from a session (implicit)
HttpSessionListener - notification of when a session is created/destroyed HttpSessionActivationListener - notification of when a session passivates/activates across VMs

Tagging Interfaces

SingleThreadModel – indicates only one Servlet instance per thread (servlets still execute in parallel)

Filter methods

init(filterConfig)
doFilter(request, response, chain)
destroy()

Authentication Schemes

  Encoding Secure Customisable Support
BASIC Base64 No No All
DIGEST Encrypted Yes No Not all
FORM Text No Yes All
CLIENT-CERT SSL Yes   All

 

Deployment Decriptor (WEB-INF/web.xml)

  • web-app
    • [display-name]
    • [description]
    • [distributable] – for apps that can be distributed across multiple VMs
    • [context-param]*
      • [description]
      • param-name
      • param-value
    • [listener]*
      • listener-class
    • servlet*
      • servlet-name
      • servlet-class
      • [display-name]
      • [description]
      • [icon]
      • [init-param]*
        • [description]
        • param-name
        • param-value
      • [security-role-ref]*
      • [load-on-startup]
    • servlet-mapping*
      • servlet-name
      • url-pattern
    • [filter]
      • filter-name
      • filter-class
      • [display-name]
      • [description]
      • [icon]
      • [init-param]*
        • [description]
        • param-name
        • param-value
    •  [filter-mapping]
      • filter-name
      • url-pattern | servlet-name
    • error-page*
      • error-code | exception type
      • location
    • [session-config]
      • session-timeout – minutes (<=0 means no timeout)
    • [login-config]
      • auth-method (BASIC, DIGEST, CLIENT-CERT, FORM)
      • [realm-name] – for BASIC
      • [form-login-config] – for FORM
        • form-loging-page
        • form-error-page
    • [security-constraint]
      • [display-name]
      • web-resource-collection*
        • web-resource-name
        • [description]
        • url-pattern*
        • http-method* (GET, POST, etc)
      • [auth-constraint]
        • [description]
        • role-name*
      • [user-data-constraint]
        • [description]
        • transport-guarantee (NONE | INTEGRAL | CONFIDENTIAL)
    • [taglib]
      • taglib-uri – user-defined unique URI
      • taglib-location – path to TLD file or JAR
See also our guide to JSPs

© 2005 i-Lab Limited