Over the time I have realised, that though I have been working on Spring framework for quite some time, I still have doubts related to how a simple servlet became so much more? Apparently, having the knowledge of the classes and methods (of a framework) that should be used as per the scenario, is sometimes not enough, specially when designing architectures of complex projects.

Let us start with the basics. I understand some of us here are advance users, but just wanted to take time and explain every step. In case you feel, you already know a topic, please feel free to jump to the next one.

What is Web Server?

Web-Server as the name suggests in used to transfer, or we can say “serve” pages to the client. A client, here is anyone typing a particular url and getting a response in form of HTTP response. It might be a page, json, xml, anything or sometimes nothing, except some http headers and response code. All this communication is happening over HTTP, which basically defines the format of request & response message.

web-server-spring-architecture
Source: https://dzone.com

What is Servlet Container?

Honestly, serving a plain static page sounds boring, thus ServletContainer came into picture. The ServletContainer make use of the power of Java to generate dynamic pages on the server side. Typically they run over a single JVM, but can run over multiple JVM by placing some configurations. Most of the modern day web servers come with in-built containers e.g. Tomcat, Jetty, Glassfish, are just a few of them you might have heard of. Such application servers are in-fact a combination of servlet containers & web servers.

web server & servlet container
Source: https://dzone.com

Just to name one, “Catalina” is a servlet container of tomcat, following the  Sun Microsystems‘s specifications for servlet and JavaServer Pages (JSP).

What is Servlet?

Well Servlet is basically an interface, with three very essential methods for its life-cycle.

  1. init( ) – This method is invoked during the initialization stage. For the initialization parameters of the web application, it uses an implementation of  ServletConfig interface.
  2. service( ) – This method does the actual job of serving the request. Every request that is made is serviced in its own thread. The web container (servlet container), calls the service() method of the servlet for each and every request.
  3. destroy( ) – This method is called when we need to destroy the servlet object and release the resources.
Image result for servlet
Source: http://idlebrains.org/tutorials/java-tutorials/servlets-init-service-destroy/

What is web.xml?

This file is the deployment descriptor for a Servlet-based Java web application, which instructs the servlet container (tomcat for ex.) which classes to load, what parameters to set in the context, and how to intercept requests coming from browsers. After servlet 3.1 many of the web.xml parts are optional, and it also allows web application configuration using Java classes using the WebApplicationInitializer.

What is WebApplicationInitializer?

This is an interface which provides a programmatic way to configure the DispatcherServlet & ContextLoaderListener in Servlet 3.0+ complaint web containers. This can act as a replacement for the web.xml file. This is any day more helpful for its support of strong typing, in oppose to plain heartless xml configurations.

ServletContainerInitializer interface introduced with Servlet 3.0 specification, is implemented by SpringServletContainerInitializer class present in spring-web*.jar. This class has an annotation @HandlerTypes with a value of “WebApplicationInitializer“, which means that the Servlet container will scan for all the classes implementing this interface and call the onStartUp method of these classes and that is where the WebApplicationInitializer fits in.

What is DispatcherServlet?

The DispatcherServlet is a servlet which takes all the incoming request, & find the right combination of handlers to redirect the request and call the specific method for the execution of request. In most of the cases it the handler is a Controller. This is the spring’s implementation of Front Controller Pattern.

enter image description here
Source: https://stackoverflow.com/questions/2769467/what-is-dispatcher-servlet-in-spring

What is BeanFactory?

The BeanFactory is the central registry of application components. It is an interface used to interact with the spring bean container. I think the most important part to mention here is BeanFactory initialize a bean, when we call getBean() method. The point to remember here is if we want to use autowiring then we have to configure AutoWiredBeanPostProcessor. Also BeanFactory only comes in handy for either testing purposes or for cases where we are crunch on resources and do not want to load all the beans with application initializing phase. For all other scenarios, ApplicationContext is more preferable.

What is ApplciationContext?

ApplciationContext is one of the basis of the IoC container of spring framework. It manages lifecycle of all the beans registered with it. In a single application, ApplicationContext can also have hierarchies. There hierarchies help to reuse beans – as a bean defined in the parent ApplicationContext can be accessed in any of its child. When a particular (type/name) of bean is required, the search begins with the context directly associated to it, if in case there was no bean in the ApplicationContext matching the bean in question, it searches for the same in the parent ApplicationContext.

What is ApplciationContextInitializer?

The ApplicationContextInitializer class is used for instances where we need to execute some code before the Spring context is completely created, one of examples may include loading properties file. It basically is used to programmatically initialize application context. One of the other major use case would be to activate profiles with the context environment.

Please let us know, if there is anything specific you need to understand or focus on, and may be we have something to offer.