All you should know about Load On Startup concept of Servlets - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

All you should know about Load On Startup concept of Servlets

Share This


While developing a web application with Java, as per your business logic, situations will come when you need to do some pre-decided time-consuming initialization jobs like Database Initialization or some external file download, etc. at the very first when your application gets loaded into the servlet container.
In these scenarios, to make your life easy, JEE framework provides a nice little concept of servlet Load On Startup.

Assuming that you are using java 5.0 or higher, you can use webServlet annotation along with its attribute loadOnStartup = 1 to explicitly direct your container to load your servlet at the very first before any other servlets of your application get loaded.

Let us suppose that your application has got three servlets namely OneServ, TwoServ, and ThreeServ. Following are the code snippets for these three servlets.

Code snippet for OneServ
@webServlet(urlPattern= "/first") public class OneServ extends HttpServlet{ // Your Servlet code }
Code snippet for TwoServ
@webServlet(urlPattern= "/second", loadOnStartup = 1) public class TwoServ extends HttpServlet{ // Your Servlet code }
Code snippet for ThreeServ
@webServlet(urlPattern= "/third") public class ThreeServ extends HttpServlet{ // Your Servlet code }

The above code snippets will allow container to load servlet TwoServ before OneServ and ThreeServ servlets. This has to happen as the webServlet annotation for TwoServ has attribute loadOnStartup with value equals to 1, while the other two servlets have no specified loadOnStartup value with webServlet annotation.

So whatever code you write in TwoServ will run before the codes written in OneServ or ThreeServ.


For your holistic learning, let us discuss some fundamental concepts related to loadOnStartup attribute of webServlet annotation
  • loadOnStartup attribute will always accept an integer. So it can have any values like 0 or 1 or -10 or 50 etc.

  • If a servlet has loadOnStartup value 0 or negative or if the loadOnStartup attribute is not present at all (as in case of servlets OneServ or ThreeServ), then the container will load the corresponding servlets as and when required, typically at the very first time, it is accessed.

  • If any two servlets have loadOnStartup values declared as x and y (x > y), the container will ensure that the servlet with loadOnStartup value x gets loaded only after the servlet with loadOnStartup value y has been loaded. So any servlet with loadOnStartup value 1 will always load at the very first.

  • A servlet that has explicitly declared loadOnStartup value in webServlet annotation will always call its init() method once it is loaded into the memory.


All the concepts presented above can thoroughly be demonstrated with the following working codes of a sample application. But before you start walking through it; please note the application structure as below



This sample application (LoadOnStartUpExample as you can see in the figure above) has three servlets OneServ, TwoServ, and ThreeServ. OneServ has no specified loadOnStartup value whereas TwoServ and ThreeServ have loadOnStartup values 1 and 2 respectively.

OneServ (which reverses a name taken as input) is accessed from a webpage index.html through a form submission, but before it is accessed from the web page, as the application is already loaded into the container, first TwoServ will be called, then ThreeServ, as the loadOnStartup values are 1 and 2 respectively. Before you submit the form in index.html to call OneServ, please check your console to check that indeed TwoServ and ThreeServ are called in proper order.

Coding index.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>BunksAllowed-LoadOnStartUp Example</title> </head> <body> <h2>Load On Start Up Example</h2> <p> When you submit the following form, a servlet named <b>OneServ</b> will be called and this servlet will show the input name in reverse order.<br> Please note this <b>OneServ</b> has no specified loadOnStartup value. </p> <p> This little sample application has two more servlets <b>TwoServ</b> and <b>ThreeServ</b>. <b>TwoServ</b> has a specified loadOnStartup value <b>1</b> and <b>ThreeServ</b> has a specified loadOnStartup value<b>2</b>. </p> <p> Hence at this instance, when you are watching this web page, as the application has already been loaded into the container, first <b>TwoServ</b> has been called followed by <b>ThreeServ</b>. The proof of their calling can be verified into your console. Hence please look into the console before submitting the below form. </p> <p> Now if you submit the following form, <b>OneServ</b> will be called as usual. Hence the order of accessing the servlets will be <b>TwoSrv</b>, <b>ThreeServ</b> then <b>OneServ</b>. </p> <br> <form action="OneServ" method="get"> <label>Enter your name to get it revered</label> <br> <input type="text" name="nameInput" /> <input type="submit" value="Submit to OneServ" /> </form> <div align="center"> <p>© <a href="http://www.bunksallowed.com">BunksAllowed</a> 2019</p> </div> </body> </html>


Coding OneServ.java
package com.bunksallowed.example.lose; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class OneServ */ @WebServlet("/OneServ") public class OneServ extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public OneServ() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("nameInput"); String reversedName = ""; for(int i = name.length() - 1; i>= 0; i-- ) reversedName = reversedName + name.charAt(i); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html> <head> <title>Name In Reverse Order</title> </head> <body>" + reversedName + "</body> </html>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }


CodingTwoServ.java
package com.bunksallowed.example.lose; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class TwoServ */ @WebServlet(urlPatterns="/TwoServ",loadOnStartup=1) public class TwoServ extends HttpServlet { private static final long serialVersionUID = 1L; public void init() { System.out.println("This is init() of TwoServ, it has loadOnStartup value 1"); } /** * @see HttpServlet#HttpServlet() */ public TwoServ() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }


Coding ThreeServ.java
package com.bunksallowed.example.lose; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ThreeServ */ @WebServlet(urlPatterns ="/ThreeServ", loadOnStartup=2) public class ThreeServ extends HttpServlet { private static final long serialVersionUID = 1L; public void init() { System.out.println("This is init() of ThreeServ, it has loadOnStartup value 2"); } /** * @see HttpServlet#HttpServlet() */ public ThreeServ() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }


If everything goes fine, once you run this application, you will find this web page as following


Now, before submitting the form on the web page, you can check your console, it will look something like the following figure. Here you will see that the servlet with loadOnStartup value 1 has been called first, followed by the servlet with loadOnStartUp value 2.


Now, if you submit this form the sample application will run as usual and give you a web page showing your name in reversed order.

Hope, you have enjoyed learning with this tutorial. More on queue.

Happy Exploring!

No comments:

Post a Comment