-2

Servlet container implements web component contract of Java EE specification, specifying a runtime environment for web components that includes security, concurrency, lifecycle management, transaction, deployment, and other services.

Apache tomcat is one open source example.

Object satisfying the contract given by javax.servlet.ServletContext, is used per each web application


Spring IOC container also implement web component contract of Java EE specification.

Object satisfying the contract given by org.springframework.context.ApplicationContext, is used per each web application


Why Spring IOC container is preferred over servlet container?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 2
    *Spring IOC container also implement web component contract of Java EE specification*: no, not at all. Spring MVC, the web framework which is part of the Spring framework, relies on the servlet specifications, and runs in a servlet container. – JB Nizet Sep 30 '17 at 19:28
  • 1
    Preferred by whom? Apart from your question being based on at least two misconceptions? – user207421 Sep 30 '17 at 20:28
  • @JBNizet When you say, Spring MVC, are you referring to only Servlet module, from [here](https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch01s02.html)? – overexchange Sep 30 '17 at 21:07

1 Answers1

3

There are many problematic statements in this question, I'll try to do my best to clarify few things by adding some "list" of facts that hopefully will help:

  1. Tomcat does not implement JEE specification, in fact, its famous only for implementing small (although important and widely-used) specification under the umbrella of JEE: servlets (and JSPs which a technically a servlet but in a more HTML like form).
  2. That's true that tomcat also follows a deployment model of WARs described in JEE, but in JEE there are much more types of archives that tomcat has nothing to do with. In addition, recent versions of tomcat allow "embedded mode", where you don't have to work with WARs at all.
  3. Spring, in a nutshell, is an IOC container, something that tomcat doesn't cover at all. I assume you know what is IOC, so I won't dig into IOC in this question.
  4. In addition to IOC, spring provides a fairly good integration with many different technologies (think about it as yet another thing that Spring can do). Now among these technologies, you can find Web Framework (called spring MVC), various templating engines (just like JSP), REST, working with database (Spring Data), Security model (Spring security) and many other things. All this makes spring a competitor of any implementation of JEE specification. Spring conceptually makes everything that JEE does (and arguably even more) but doesn't follow the standards established by JEE. Having said that, in modern versions, the difference becomes less and less significant, in many cases, Spring does honor interfaces and annotations offered by JEE, in addition to its own way to do stuff.

  5. So, now it's obvious that Spring and Tomcat don't really compete with each other. In fact, they can work together and its a really wide-spread tandem: One can use a spring framework to develop an application that will run on tomcat. Another successful model is to use spring boot that allows embedding the tomcat into the spring boot application and use it under the hood to server web (HTTP/Rest) requests.

Hope this sheds some light and helps to understand the differences between Spring and Tomcat

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Why Spring MVC web framework is preferred over JSP's? – overexchange Sep 30 '17 at 20:03
  • Servlets are low level. They give an abstraction over HTTP protocol. So you work with notions like request, response, header, query parameter and so on. MVC is built on top of servlets and provides much more high level set of abstractions : model objects, applicative controllers, routing between them, etc. – Mark Bramnik Sep 30 '17 at 20:17
  • @overexchange Spring MVC is also dramatically more flexible, supporting multiple template systems (I prefer Thymeleaf over JSP) and other REST representations such as JSON and XML (basically including what Jersey does). – chrylis -cautiouslyoptimistic- Sep 30 '17 at 20:28
  • 1
    Wrt 3rd point, Yes, I know that Spring IoC container implemented using DepInjection pattern. I implemented IoC using service locator pattern in [C_code](https://github.com/shamhub/Design_pattern/tree/master/Service_locator_pattern). In C world we use service locator pattern to implement IoC. IoC in C world is mostly used for plugin architectures(Ex: paint program, snmp agents etc..). What is the idea of Spring to follow IoC in Java world? Servlet container also follows IoC principle using `web.xml` url pattern matching, to pick correponding servlet classes. Isn't it? – overexchange Sep 30 '17 at 22:20
  • Probably in spring it takes many steps further. Spring allows to manage all your objects by itself. Since you come from C that doesn't support OOP, expect to see hundreds, thousands and even more beans (spring bean is a object managed by spring) in your system. Why doing that? Many reasons : spring managesclifecle, handles initialization, makescyou writing code in a way that can be tested and so on. In small examples you wont see it, but in bigger projects it will become more obvious. – Mark Bramnik Oct 01 '17 at 04:11
  • So, when we say spring IOC container, the container is helping allow the business logic to follow IoC principle using DI pattern. But spring IOC container does not mean, container is implemented using IOC principle. Isn't it? – overexchange Oct 01 '17 at 16:37
  • I'm not sure I follow your thought, but I can say Tomcat and spring are just two different technologies for two different purposes. By coincidence, they both referred as "containers", again different containers for different content... Spring does IOC for all classes that you wish to be handled by spring. Tomcat just allows responding web requests – Mark Bramnik Oct 01 '17 at 18:55
  • I was trying to say that, we say, Spring IOC container as IOC container not because of the reason that container itself is implemented using IOC principle. we say, Spring IOC container as IOC container, because it allows us to write applications(using beans) that follow IOC principle. Isn't it? – overexchange Oct 03 '17 at 10:31
  • In 2002, Why *Pivotal* thought about designing Spring IOC container to enable IOC principle in our applications? What is the advantage of using IOC principle in designing enterprise application? Basically I come from C background where we implement IOC principle using [service locator pattern](https://github.com/shamhub/Design_pattern/tree/master/Service_locator_pattern), for plugin architectures like snmp agents, paint program etc... – overexchange Oct 04 '17 at 20:28
  • IOC container is just yet another tool. It turned to be very successful though. I use it (as a java dev.) because it handles a lot of "boilerplate" and configuration code for me: just define a bean and spring will take care of its lifecycle, inject by itself its dependencies and will let me concentrate on business instead of on "infrastructure". Another reason is using DI makes the code easily unit testable. When it comes to testing, java ecosystem offers one of the most advanced sets of technologies and they play extremely well with them. These are my reasons (subjective opinion :) ) – Mark Bramnik Oct 05 '17 at 07:02
  • As mentioned [here](https://stackoverflow.com/a/7295697/3317808), *Spring doesn't necessarily require Java EE to run. A barebones Servletcontainer like Tomcat is already sufficient.* But to run JEE, I need application server(has servlet container) like *Glasssfish*. Why servlet container is not sufficient to run JEE(EJB, servlets, CDI)? – overexchange Dec 20 '17 at 23:56
  • Merely because Tomcat is not intended to run all these JEE technologies, it wasn't in a scope of its authors. For example, let's take EJB. In order to be able to be run EJB (session beans) - the container has to "understand" that the class is actually an EJB, put it into JNDI tree, manage its lifecycle and so forth. Tomcat doesn't know how to do it. – Mark Bramnik Dec 21 '17 at 05:01