Components

Servlet Container

The servlet container is the runtime environment for Java servlets. It is responsible for managing the lifecycle of servlets and mapping requests to servlets. It also manages access to static resources such as HTML files or images and private resources such as JAR files or class files.

Resources are grouped into two categories:

  • Private: Direct access not possible, requires identification and permission.
  • Public: Direct access possible, no identification or permission required.

Servlet Context

The servlet context is the application context for Java servlets. It is responsible for providing access to the application runtime environment and all required dependencies (DI and Injection in SprintBoot).

The context allows to access initialization parameters and attributes.

Servlet

The servlet is responsible for handling requests and generating the responses. It is the main component of a Java servlet web application.

Documentation:

  @WebServlet(urlPatterns = "/*")
public class BasicServlet extends HttpServlet {
    @Override
    protected void init(ServletConfig config) throws ServletException {
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("Hello World!");
    }
}
  

Filter

Filters are responsible for preprocessing requests and postprocessing responses. They can be used to implement authentication, logging, compression, encryption, and much more. The have to be registered in the web.xml file.

The main design patterns used in filters are: Interceptor and Chain of Responsibility.

Documentation:

  @WebFilter(filterName = "BasicFilter", urlPatterns = "/*")
public class BasicFilter extends HttpFilter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // one-time initialization
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // code to execute before the servlet (request)
        chain.doFilter(request, response);
        // code to execute after the servlet (response)
    }

    @Override
    public void destroy() {
        // one-time cleanup and remove filter from chain
    }
}
  

Listener

Listeners are responsible for handling events that occur in the servlet container.

  @WebListener
public class BasicListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // code to execute when the servlet context is initialized
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // code to execute when the servlet context is destroyed
    }
}
  

Deployment Descriptor

The WEB-INF/web.xml file is called the deployment descriptor. It is used to configure the servlet container and the servlet context.

  <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="3.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 
    <servlet>
        <servlet-name>BasicServlet</servlet-name>
        <servlet-class>ch.fhnw.webfr.web.BasicServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>BasicServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>BasicFilter</filter-name>
        <filter-class>ch.fhnw.webfr.filters.BasicFilter</filter-class>
        <init-param>
            <param-name>InitParamName</param-name>
            <param-value>InitParamValue</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>BasicFilter</filter-name>
        <url-pattern>/*</url-pattern>
    <filter-mapping>

    <listener>
        <listener-class>ch.fhnw.webfr.listeners.BasicListener</listener-class>
    </listener>

</web-app>
  

Diagram

stateDiagram-v2
    state "Servlet Container" as Container
    state "Servlet Context" as Context
    state "Static Content" as Static
    state "Dynamic Content" as Dynamic

    state Container {
        state Context {
            Static
            Dynamic

            note left of Dynamic
                Servlets, Listeners, Domain classes, Third-Party Libraries
            end note

            note right of Static
                HTML, CSS, JS, images
            end note
        }

        note left of Context
            Application context
        end note
    }

    note right of Container
        Runtime environment
    end note