Saturday, 3 May 2014

Servlets : load-on-startup - Introduction

Master Core Java & Servlets!

Subscribe to Ram N Java for beginner-friendly coding walkthroughs, web architecture lessons, and hands-on Java guides!

🔔 Subscribe to Ram N Java

What is Load-on-Startup in Servlets?

By default, servlet containers instantiate and initialize a servlet lazily—meaning the servlet is only loaded when a client makes its very first request to that servlet URL. While lazy loading saves memory during application deployment, it can introduce latency for the first user whose request triggers the heavy initialization process.

The load-on-startup feature instructs the servlet container to load and initialize a servlet eagerly when the web application or server boots up, rather than waiting for an incoming request.

How the Priority Values Work

The value assigned to load-on-startup is an integer that controls both when and in what order the servlet container initializes servlets:

  • Negative Value (or Default): If the value is negative (e.g., -1) or omitted, the container loads the servlet lazily upon receiving the first request.
  • Zero or Positive Value (e.g., 0, 1, 2...): The container initializes the servlet during application startup. Lower positive numbers have higher initialization priority. For example, a servlet configured with 1 initializes before a servlet with 2.
<!-- web.xml configuration -->
<servlet>
    <servlet-name>AppInitServlet</servlet-name>
    <servlet-class>com.ramnjava.AppInitServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Or via @WebServlet annotation in Servlet 3.0+ -->
@WebServlet(urlPatterns = "/init", loadOnStartup = 1)
public class AppInitServlet extends HttpServlet { ... }

Key Advantages of Load-on-Startup

  • Eliminates First-Request Latency: Pre-loads resources and runs the servlet's init() method during server startup so initial users experience fast response times.
  • Pre-Loading Critical Services: Perfect for servlets responsible for pre-populating caches, loading master configuration data, or warming up connection pools.
  • Predictable Initialization Order: Lets developers explicitly control dependencies and initialization sequence across multiple servlets.

Recommended Related Tutorials

Continue exploring servlet fundamentals and architecture with these tutorials from Ram N Java:


Click here to watch in Youtube : https://www.youtube.com/watch?v=oWWY86B_nUo

Click the below Image to Enlarge
Servlets : load-on-startup - Introduction
Servlets : load-on-startup - Introduction
See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials