Servlet Life Cycle - novalexei/mod_servlet GitHub Wiki

Servlet Life Cycle

Now when we saw how they work it is about time to learn about how mod_servlet container controls servlet instances.

  1. When servlet is created by the container its method init(config) is called. By default this method sets configuration and calls method init(). This method init() is a good candidate to override if some special initialization of the servlet is required.

    The instance is created when it first time requested, unless load-on-startup is specified for the servlet.

  2. When server receives client request it finds appropriate servlet and invokes service(request, response) method. By default this method dispatches the request to appropriate do_* method depending on the method of the request (for example, if the request's method is DELETE, do_delete() will be called).

  3. The instance of the servlet lives throughout the live of the server (except those instances which don't have any mappings configured). And on the server shut down destructors will be called for all existing instances.

The life cycle of filter is very similar, except filters don't support load-on-startup.

load-on-startup can be very useful when a certain servlet or the whole web application needs initialization. To perform the web application initialization we can write a servlet with only init() method overridden and configure it to load-on-startup. There even is no need to specify any mapping for this servlet, mod_servlet container will load it on startup, perform initialization and destroy the instance.

Let's check how it works:

std::shared_ptr<servlet::logging::logger> LG = servlet::logging::get_logger();

struct init_servlet : public servlet::http_servlet
{
    void init() override
    {
        LG->config() << get_servlet_name() << ":init()" << std::endl;
    }
};
SERVLET_EXPORT(initServlet, init_servlet)

Then in web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>InitServlet1</servlet-name>
        <servlet-factory>libinit.so:initServlet</servlet-factory>
        <load-on-startup></load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>InitServlet2</servlet-name>
        <servlet-factory>libinit.so:initServlet</servlet-factory>
        <load-on-startup>100</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>InitServlet3</servlet-name>
        <servlet-factory>libinit.so:initServlet</servlet-factory>
        <load-on-startup></load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>InitServlet4</servlet-name>
        <servlet-factory>libinit.so:initServlet</servlet-factory>
        <load-on-startup>50</load-on-startup>
    </servlet>
</web-app>

After you started the server check the log file of the application:

[16/11/14 17:16:22.688] [139891545212800] [CFG] (root): InitServlet4:init()
[16/11/14 17:16:22.688] [139891545212800] [CFG] (root): InitServlet2:init()
[16/11/14 17:16:22.688] [139891545212800] [CFG] (root): InitServlet1:init()
[16/11/14 17:16:22.688] [139891545212800] [CFG] (root): InitServlet3:init()

You can see that first were loaded servlets with numeric values in load-on-startup (lower numbers first) and after that servlets which have load-on-startup without any numeric values will be initialized in the same order the appear in the web.xml file.

It looks like we are done with the tutorial. But before you get down to your advanced cool web application let me give you some general advice

To The Programming Guide

⚠️ **GitHub.com Fallback** ⚠️