Igor Gatis
Igor Gatis

Reputation: 4898

How to initialize a web app?

My Web App will be deployed as a WAR package in a Jetty instance. It needs to perform a lot of caching before serving requests. How do I call the caching method before anything else? is the a static void main() in the web app standard?

Upvotes: 5

Views: 3889

Answers (1)

leonbloy
leonbloy

Reputation: 75906

A standard (old) way is to code a Servlet which takes care of initialization stuff in its init() method. You force it to be initialized at application start by adding a load-on-startup positive value in your web.xml

  <servlet>
        <servlet-name>myinit</servlet-name>
        <servlet-class>com.example.MyInitServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
  </servlet>

Today it's more usual to have a bean container like Spring, which takes care of this kind of things (instantiating services objects, preloading cacheable sharable data, etc).

Note: this recipe is for webapps in general, not specific to Jetty.

Upvotes: 5

Related Questions