skip to Main Content

I’ve been reading and seeing examples about servlets, servlet containers and web frameworks for the JVM. I’m now wondering, what’s the point of using a servlet container instead of something like netty?

I’m not 100% I understand it correctly, but a way you can deploy a website backed by Java is by writing either servlets or better yet something that uses servlets but abstract them away, so you can choose your favorite servlet container.

But there are frameworks like Spark and ktor that don’t seem to require a servlet container, and can run on things like netty, yet as far as I know you can run them on a servlet container if you want to… That’s confusing to me: if I already have a web application that can run and binds itself to a port to work, and has a working engine to handle the networking IO, why would I do that? To be able to use servlet filters and other standarized things? Is there an issue if I just have a netty-backed app behind nginx?

I think I know what a servet and servlet container are, but I don’t understand what they offer (just some standarization?)

2

Answers


  1. Servlets exists due to historical context. In the past, Java EE Web Servers were a popular way of running web services such as WebLogic, JBOSS …etc. What we used to do is to create a java service that abides by servlet specification and then any of these Java EE Web Servers could be able to run it.
    I personally prefer a modern approach that doesn’t rely on Servlet specification because modern approaches offers better performance.

    Login or Signup to reply.
  2. A servlet container is needed when your project uses a Java Servlet based web framework and/or the project produces a WAR file. This is simply because a servlet container is essential in order to parse web.xml and/or to register and run a @WebServlet/@WebFilter/@WebListener annotated class and/or to deploy a WAR file to a live environment.

    Examples of Java Servlet based web frameworks are:

    • Jakarta Faces
    • Spring MVC
    • Struts
    • Vaadin

    If you however don’t use any Java Servlet based web framework (i.e. you don’t use jakarta.servlet.* dependencies anywhere, also not transitively) and the project doesn’t produce a WAR file but a JAR file, then you can simply use an embedded server which is executable from a main() method such as Netty along with a Java based framework to respond on HTTP requests. These are essentially called Java Microservice frameworks and they are more than often used in combination with a JavaScript based web framework such as React and Angular.

    Examples of such Java Microservice frameworks are:

    • Jakarta REST
    • Quarkus
    • Vert.x
    • Micronaut
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search