Java EE 7 Application Developer study materials
Java EE 7 Application Developer study resources

Java EE 7 Application Developer: Servlets and JavaServer Pages (JSP)

Java EE 7 Application Developer · Work through your exam, one topic at a time.

All guide sections

Servlets and JavaServer Pages (JSP)

Domain 2 tests the servlet contract: one instance per servlet definition serving many threads, a strict lifecycle, and a request/response model in which the commit point governs what you may still do. JSP questions usually reduce to the generated servlet, EL scope resolution and JSTL tags.

Lifecycle and configuration

  • The container calls init(ServletConfig) once, then service() per request on a shared instance, then destroy(). Implement doGet/doPost and never keep per-request state in fields.
  • @WebServlet, @WebFilter, @WebListener and @MultipartConfig replace web.xml, but a descriptor entry overrides the annotation.
  • ServletConfig parameters are per servlet; ServletContext parameters are application-wide. Both are Strings.

Dispatch and session

  • RequestDispatcher.forward() is server-side, stays in the same context, keeps request attributes and must run before the response commits; sendRedirect() creates a new request and loses attributes.
  • HttpSession is tracked by the JSESSIONID cookie or URL rewriting; invalidate() discards it. Attributes set on the session persist across requests in that session.

Async and JSP

  • asyncSupported=true must be set on the servlet and on every filter in the chain, otherwise startAsync() throws IllegalStateException. Servlet 3.1 adds non-blocking I/O through ReadListener and WriteListener.
  • A JSP is compiled into a servlet; EL resolves pageScope, requestScope, sessionScope then applicationScope in that order, and JSTL supplies iteration and formatting tags.

Worked example

A filter calling request.startAsync() throws IllegalStateException even though the target servlet declares asyncSupported=true. The chain rule is the cause: once a non-async filter has processed the request, async is disabled for the whole dispatch, so that filter must also carry asyncSupported=true.

Traps

  • Writing to the response and then forwarding, which throws IllegalStateException.
  • Confusing request parameters (client-supplied Strings) with request attributes.
  • Assuming annotation-declared filters run in declaration order; that order is not guaranteed.