Are you new to the Java web development world? Do you hear senior developers throw around the “web container”, “Java server”, “servlet container” terms ? Do you only have a vague understanding of what that means, and what the difference is ? Let’s try to clear things up.
What do Java applications run on ?
Java applications run on the Java Runtime Environment. The JRE is what allows Java applications to be cross-platform : write once, run anywhere. Any machine which has a JRE installed will be able to run your software.
In the case of Java EE applications, that is to say web applications, an extra piece of software is required: a Java EE server which will take care of handling requests, cookies and authentication, and more for your web application. The Java EE server runs on the JRE, and takes care of executing your Java application.
What are the different types of servers and containers for my Java EE application ?
A Java EE server contains a web container (aka servlet container) and an Enterprise Java Beans container.
A web container manages the execution of Java Server Pages (JSP) pages and servlet components. Basically, this means it can run your web application. JSP is a way of rendering web pages on the server-side, which can make it easier to display Java objects and to avoid some security issues. (It’s mostly used in old projects nowadays; few new projects use JSP). Nonetheless, the web container is still necessary for execution of any form of web application, including more modern single-page applications, or web applications where the standalone frontend queries the backend’s API.
An EJB container manages the execution of enterprise beans. Together, the web container and the EJB container make up the Java EE server.
Containers and extensions
A web container requires a Web Archive (.war) file. This is a standard Java Archive (.jar) file, renamed to .war, which contains a WEB-INF/web.xml file (the web deployment descriptor).
An EJB container requires a Java Archive (.jar) file. It contains a ejb-jar.xml file (the EJB deployment descriptor).
Java EE server requires an Enterprise Archive (.ear) file. This is simply a wrapper around a .jar or .war file. Some Java EE servers accept that you upload a .jar or .war directly, and create the .ear around it internally.
Standalone web containers
It’s important to note that web containers and EJB containers can exist on their own. If you are only hosting a web application, you can choose a web container to host your application, instead of taking a heavier full Java EE server. The most used standalone web container is Apache Tomcat, which also exists as an embedded version in Spring Boot applications.
- Java EE server = web container + EJB container
- Most frequently used web container: Apache Tomcat, also used implicitly in Spring Boot applications
- Java EE server: .ear
- Web container: .war
- EJB container: .jar
Sources & further reading
Container Types: https://docs.oracle.com/cd/E19316-01/819-3669/bnabq/index.html
Packaging applications: https://docs.oracle.com/javaee/7/tutorial/packaging001.htm