Wednesday, 11 January 2017

What is load-on-startup In Servlet ?


Load-on-startup:

Servlet Container loads the servlet on the first request, but if the servlet is heavy, then it takes much time to loads. Then we can load those servlet on application starts. 

So we can load heavy servlet on application startup in two ways i.e. first way is to use <load-on-startup> tag under the <servlet> configuration tag in web.xml, and second way is to use @WebServlet(loadOnStartup=1) annotation over the servlet name for annotation based configuration.

In load-on-startup configuration we provide integer values ,if it is negative(-) value then servlet container will load the servlet on request, if it is positive values i.e. from 0 then servlet container will load servlet on application startup.

If multiple servlet configuration contains load-on-startup configuration and having values such as 0,1,2,3,4 and so on then servlet container will starts loading servlet from lower integer values and so on.

Web.xml Configuration Example:

1
2
3
4
5
<servlet>
     <servlet-name>HomeSevlet</servlet-name>
     <servlet-class>org.servlet.HomeSevlet</servlet-class>
     <load-on-startup>1</load-on-startup>
</servlet>

Annotation Configuration Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@WebServlet(value="/HomeSevlet", loadOnStartup=1)
public class InitializeResources extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      System.out.println("HEREEEE");
  }

}


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment