Java Web Application Development Evolution: Servlet -> Spring -> Spring Boot

Aditya Rao CAditya Rao C
20 min read

Servlet

Before the release of spring and spring boot, the primary way of making web applications in Java was by using Java Enterprise Edition which was renamed to Jakarta Enterpise Edition in 2017. The package names for the corresponding libraries were also changed to begin with jakarta rather than javax. For making java web apps using this approach we had to write subclasses of jakarta.servlet.http.HttpServlet class for handling HTTP request messages and then compile and package our java .class files into a WAR (Web application archive) file and then give this WAR file to a separately installed program called a servlet container which will store and execute our servlet code and allows us to access our web app. There are many servlet containers available , examples include: Apache Tomcat, Eclipse Jetty, Undertow, GlassFish and many others. Here we will go through a simple example of making a servlet based web app and use Apache Tomcat as the servlet container.

We will be using IntelliJ IDEA ide and Maven build tool for all the coding examples in this article. There is no need to install maven separately if you are using intelliJ ide as it already has maven integrated with it. Open intellj and click on create a new project or File→New→Project in the main menu bar if you already previously opened a project in intellij. Then click on Maven Archetype in the Generators section present on the left bar. Set the name as ‘jakarta-servlet-web-app’ (or any name you like where each word is separated with hyphens). The name field will prevent you from creating the project if you use spaces as this name value will be used as the artifact id in the pom.xml file and this artifact id should not contain any spaces. Set the location to the path where you want to create this project’s folder (or leave it to the default location). Set the JDK that you want to use (should be auto filled if you have a JDK installed in your system). Leave the Catalog option to it’s default value which is ‘Internal’. Set the archetype to ‘org.apache.maven.archetypes:maven-archetype-webapp’ by selecting it from the drop down. Leave the version with the default value which is ‘1.0’. Then click on the create button to create your project.

Take a look at the pom.xml file. There is a <packaging> xml element which contains the content ‘war’. This instructs maven to compile and package our source code into a WAR file which is exactly what we require. If you look at the project directories you will see that main folder does not contain a java folder. Maven expects our source code to lie within src/main/java/<package-folders> where <package-folders> represents one or more folders that needs to be created by us in order to create a java package. Go ahead and create the java folder inside the main folder by right clicking the main folder and hovering over ‘new’ and then clicking ‘directory’ and then typing name as ‘java’. After that right click on java folder and hover over ‘new’ and click on ‘package’ and type in the package name. I will be using ‘servlet.web.app’ as the package name but you can use anything else that you like. Now create a java class inside the package you just created and name it anything you like. I will be naming it as DemoServlet. In the end your project structure should look like this:

Now we need to tell maven to download the jakarta servlet library. You can get the groupId, artifactId and the version of the jakarta servlet library from the maven central repository website. As of the time of writing this article the latest version is 6.1.0. Here is the updated pom.xml file with jakarta servlet library added as a dependency:-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>jakarta-servlet-web-app</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>jakarta-servlet-web-app Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>jakarta-servlet-web-app</finalName>
  </build>
</project>

We are now able to create a servlet by making any class inherit from the jakarta.servlet.http.HttpServlet class. So let us make our DemoServlet class into a servlet by extending from the HttpServlet class and also override HttpServlet’s doGet() method.

Updated DemoServlet.java:-

package servlet.web.app;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class DemoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

The doGet() method handles http get requests and is given two arguments. HttpServletRequest represents the http request message and the HttpServletResponse represents the http response message. There are other do<http-request-method>() methods present within the HttpServlet class that you can override in order to handle other types of http requests. Take a look at the documentation if you want further details. In this simple example we will only handle a http request message with a get request method. Inside the doGet() method, let us write a string to the http response message’s body.

Updated DemoServlet.java:-

package servlet.web.app;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class DemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello from jakarta servlet web app");
    }
}

So now we have a servlet that handles a http request message with a get request method by returning a http response message that contains a string in its body. Now we need to associate this servlet with a resource path. We can do this by using the jakarta.servlet.annotation.WebServlet annotation and specifying the resource path in it’s urlPatterns element which accepts a String[] as its value.

Updated DemoServlet.java:-

package servlet.web.app;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(urlPatterns = {"/hello"})
public class DemoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("hello from jakarta servlet web app");

    }

}

We are now all done with coding up the servlet. This annotation that we just used has another benefit too. Before this annotation and several other configuration annotations were added in the jakarata servlet library we had to specify all of our web app’s servlets classes along with their associated resource paths and other components within a web.xml file which had to be present within the src/main/webapp/WEB-INF directory. You will see that the web.xml file has been automatically created for us by intellij but thanks to addition of configuration annotations such as @WebServlet in the jakarta servlet library we can avoid touching the web.xml file for configuring our servlet based web app. We can call this approach of configuration as annotation based configuration or java code based configuration.

Now let us make the maven build tool compile and package our code as a WAR file. We can do this through the intellij maven sidebar. Open the maven sidebar by clicking on the ‘m’ symbol on the right side and then clicking on ‘LifeCycle’ to expand all the possible options and then clicking on ‘package’.

Alternatively first open the maven sidebar and then click on the ‘execute maven goal’ option and then type in mvn clean package and press enter.

Both of the above actions cause the WAR file to be created within the target directory.

Our servlet based java web app is ready. In order to run the web app we need a servlet container program. We will download and use apache tomcat. You can download apache tomcat from the official website:- https://tomcat.apache.org/. At the time of writing this article the latest version is 11.0.7. Here’s the download link for zip file (if using windows):- https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.7/bin/apache-tomcat-11.0.7.zip. Here’s the download link for tar.gz file (if using linux or macOS):- https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.7/bin/apache-tomcat-11.0.7.tar.gz . I downloaded the zip file as i am using windows. Extract the folder present inside the zip or tar.gz and place it wherever you want. This folder contains the apache tomcat servlet container program and the name of this folder is of the format:- apache-tomcat-<version no>. Now open that folder and go inside the ‘webapps’ folder. This folder as the name suggests is used to store all the web applications so that apache tomcat can run them. Copy the WAR file which contains our web app (here the name of the war file is ‘jakarta-servlet-web-app.war’) to this ‘webapps’ folder.

Now we need to start apache tomcat so that it can run our web app. You can start apache tomcat by using the startup.sh shell script if you are on linux or macOS and the startup.bat file if you are on windows. I will be using the startup.bat file. Both of these files are present within the ‘bin’ directory inside the ‘apache-tomcat-<version-no>’ folder. Open command prompt or terminal within this bin folder and type ‘startup’. You should see another terminal window automatically getting opened which contains messages displayed by apache tomcat which indicates that apache tomcat program has started.

By default apache tomcat listens on 0.0.0.0 and port 8080. Which means that you can access it using localhost or your private ipv4 address. Now open your favorite web browser and type “http://localhost:8080” into the address bar. You should see apache tomcat’s default web page.

Now go and take a look inside the ‘webapps’ directory again. You will see that apache tomcat has automatically created a folder with the same name as the war file.

This folder represents our servlet based web app. In order to run our webapp type in ‘http://localhost:8080/<folder-name>/hello’ in the browser address bar where <folder-name> is the name of the folder representing our web app (here it is ‘jakarta-servlet-web-app’ so the url would be ‘http://localhost:8080/jakarta-servlet-web-app/hello’). Our DemoServlet class handles http get requests for the resource path ‘/hello’ so that is why we have to include /hello in the url so that our DemoServlet’s doGet() method runs. You will see the text ‘hello from jakarta servlet web app’ in the browser window which means that our DemoServlet’s doGet() method was run.

We have successfully created a servlet based web app and ran it inside apache tomcat which is a servlet container program.

Spring

The Spring Web MVC library makes it more easier to code up a java web app. It builds on top of the jakarta servlet library and allows us to code http request message handling methods in classes annotated with org.springframework.web.bind.annotation.RestController annotation instead of using classes that extend jakarta.servlet.http.HttpServlet. A spring based web app still runs on a servlet container program like apache tomcat. But you might think how is that possible as we won’t be coding up any servlet classes and instead will be coding up classes annotated with @RestController and servlet container programs need servlets in order to run the web app. This is why spring web mvc library provides a org.springframework.web.servlet.DispatcherServlet class which indirectly extends the HttpServlet class. We don’t code up any servlets but instead configure spring web mvc’s DispatcherServlet as the only servlet of our web application. While initialising the DispatcherServlet object we pass the spring IOC container(application context) to it’s constructor. Beans (spring managed objects) will be created for each of the @RestController annotated classes in the spring IOC container. So when a http request message arrives from a client, it first goes to the DispatcherServlet and then the DispatcherServlet goes through the @RestController annotated classes inside the spring IOC container in order to find the appropriate method for handing the http request message and sends the message to that method after which the method executes and sends a http response message back to the DispatcherServlet which then sends the response message to the client that sent the http request message. Let us now code up a simple spring web mvc web app.

The process of creating an intellij idea project is the same. We will only keep a different name for the project this time and set it as ‘spring-web-mvc-web-app’. We will be using spring framework version 6.2.7 in this project which is the latest version at the time of writing this article and it requires JDK 17 or higher. So you will have to download a higher JDK version if your currently installed JDK version is less than 17 in order to continue with this project.

After creating the project, follow the same steps as described in the Servlet section to create the java folder and package within but this time give the package name as ‘spring.web.mvc.app’. After creating package, create 2 classes inside the package one with the name ‘AppConfig’ and other one with the name ‘DemoController’. The project directory structure show look like this:-

Now we will add the dependencies for spring web mvc, jakarta servlet and jackson databind in the pom.xml file. Jackson is a library that is used for converting java objects to json (serialization) and also for converting json to java objects (deserialization). By adding jackson library to our dependencies, spring web mvc will automatically use it to convert whatever we return from the @RestController class’s methods to json and then add it to the response message’s body. Here’s the pom.xml file after the dependencies with all the dependencies added:-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>spring-web-mvc-web-app</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring-web-mvc-web-app Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>6.2.7</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.19.0</version>
    </dependency>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>spring-web-mvc-web-app</finalName>
  </build>
</project>

Now let us start coding up the AppConfig class. We will first add three annotations to this class.

AppConfig.java:-

package spring.web.mvc.app;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@Configuration
@ComponentScan
public class AppConfig {


}

The @EnableWebMvc annotation is used to tell spring web mvc to auto configure itself with it's default configuration. The @Configuration annotation is used to tell spring that this class is a source of bean definitions. But we are not going to be declaring any @Bean methods inside this class. The reason we have to use @Configuration is because both @ComponentScan and @EnableWebMvc annotations require the class to also be annotated with @Configuration. The @ComponentScan annotation is used to enable spring’s auto scanning for @Component classes so that it can automatically create beans(spring managed objects) for those classes. The @RestController annotation is meta annotated indirectly with @Component, so all classes annotated with @RestController inside this package will be automatically detected by spring and it will create beans(spring managed objects) for those classes.

Now we need to configure spring web mvc’s DispatcherServlet. We can do this using either java code based configuration or by using a web.xml file. Here we will use the java code based configuration. Make the AppConfig class implement the WebApplicationInitializer interface and it’s onStartup() method.

Update AppConfig.java:-

package spring.web.mvc.app;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRegistration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@Configuration
@ComponentScan
public class AppConfig implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);

        DispatcherServlet servlet = new DispatcherServlet(context);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }
}

Basically the onStartup method is automatically called by the servlet container when it starts in order to give our application the chance to tell the servlet container to register the DispatcherServlet. Inside the onStartup() method we first set up the spring IOC container by creating an object of the class AnnotationConfigWebApplicationContext class, then create a DispatcherServlet object by passing the spring IOC container (AnnotationConfigWebApplicationContext) into it’s constructor, then register the DispatcherServlet with the servlet container by using the ServletContext object that is passed as a parameter to the onStartup() method by the servlet container and then finally specify the DispatcherServlet’s load on startup priority and map it to the ‘/’ url.

Now let’s start coding up the DemoController class.

DemoController.java:-

package spring.web.mvc.app;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/hello")
    String getRequestHandler() {
        return "hello from spring web mvc web app";
    }   

}

The DemoController class is annotated with the @RestController class annotation. The @RestController annotation is meta annotated indirectly with @Component, so this class will be automatically detected by spring and a bean will be created for this class in the spring IOC container. The purpose of a class annotated with @RestController is to define http request message handling methods. Here we have created only a single method called ‘getRequestHandler’ (you can keep any name you want) which handles a http request message with a get request method. The @GetMapping annotation is put on the method in order to tell the DispatcherServlet that this method handles http get request for the resource path of ‘/hello’. There are other @<request-method>Mapping annotations for the other request method types also within the org.springframework.web.bind.annotation package. The getRequestHandler() method returns a string. So basically when a client such as a browser requests for the resource at the resource path ‘/hello’, the http request message from the client first arrives at the DispatcherServlet, then the DispatcherServlet goes through all the @RestController beans within the spring IOC container (in our example only one bean would be present) and finds a method annotated with @GetMapping(“/hello”) and then executes that method in order to get it’s return value (in our example the getRequestHandler() method gets executed), then puts the return value in the http response message’s body and sends it to the client.

We are now done with coding up our web app. Follow the same steps shown in the Servlet section of this article in order to first generate a war file within the target directory (the name of the generated war file will be ‘spring-web-mvc-web-app.war ‘ ), then give it to apache tomcat and then startup apache tomcat. Once apache tomcat has started up, open your favourite browser and go to this url by typing this in the address bar ‘http://localhost:8080/spring-web-mvc-web-app/hello’. You should see the string ‘hello from spring web mvc web app’ in the browser window.

We have successfully created a spring web mvc web app and ran it inside apache tomcat which is a servlet container program.

Spring Boot

Spring boot makes it even more easier to make a java web app. We will still be using the spring web mvc library but spring boot will autoconfigure the DispatcherServlet for us so we don’t have to define a class that implements WebApplicationInitializer like we did while coding up our spring web mvc web app in the Spring section of this article. The main advantage of using spring boot along with spring web mvc is that we don’t have to create a war file and give it to a separate servlet container program like apache tomcat as a spring boot web app will automatically create and intialize a embedded apache tomcat servlet container for us. We don’t have to separately install apache tomcat for a spring boot based web app. A spring boot based web app is like a standalone web application in and of itself. So we just have to code up our application and run it like any other java program either through a jar file or by running directly from ide or by using spring boot’s maven plugin. Let us now code up a simple spring boot web app.

This time we will use the ‘new project’ section of intellij idea’s ‘new project’ setup window to create our java project instead of using the ‘maven archetype’ section we used in the previous two sections of this article. We will be using spring boot version 3.4.5 in this project which is the latest version at the time of writing this article and it requires JDK 17 or higher. So you will have to download a higher JDK version if your currently installed JDK version is less than 17 in order to continue with this project. Here is an image of the new project setup window with all fields filled (you can set the Location field to the desired folder where you want your project folder to be created inside):-

Now let’s add the required dependencies in the pom.xml file.

pom.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-boot-web-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.5</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Notice the <parent> xml element. The parent xml element in the pom.xml is used to specify a different pom.xml file from which this pom.xml will inherit from. The spring-boot-starter-parent pom.xml file does not declare any dependencies of it’s own but includes dependency management meta data for many other dependencies so that we can specify those dependencies in our pom.xml file without specifying their version. You can check out this link to the official spring boot reference that lists all the dependencies for which the version info has been specified by the spring-boot-starter-parent pom file: https://docs.spring.io/spring-boot/appendix/dependency-versions/coordinates.html.

Declaring the spring-boot-starter-web dependency brings in many transitive dependencies. The main transitive dependencies i want to point out are spring web mvc, embedded apache tomcat and jackson. Basically it brings in majority of the dependencies we might need in order to make a web app. Apache tomcat is the default embedded servlet container provided by this dependency. Spring boot also supports undertow and jetty if you don’t want to use apache tomcat. We will stick with the default. Declaring the spring-boot-starter-test dependency brings in many transitive dependencies related to java application testing such as JUnit Jupiter,Hamcrest and Mockito. Notice that for both the dependencies the <version> element has been omitted as maven will already know the version to download due to the spring-boot-starter-parent pom.xml file’s dependency management meta data.

A plugin called spring-boot-maven-plugin has also been added. This plugin allows us to package our spring boot web app as a jar file. You can use ‘mvn clean package’ maven command in order to generate a jar file once we are done coding up our web app. This plugin also allows us to run our spring boot web app using the ‘mvn spring-boot:run’ maven command. You can execute this maven commands by opening intellij’s maven sidebar, then clicking on ‘execute maven goal’ and then typing in the command and pressing enter. We didn’t have to specify a version for this plugin also due to spring-boot-starter-parent pom.xml file’s dependency management metadata.

Now lets start coding in the Main class which was auto generated by intellij.

Updated Main.java:-

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        ConfigurableApplicationContext context=SpringApplication.run(Main.class,args);
    }
}

We annotated our Main class with @SpringBootApplication. This annotation is meta annotated with @EnableAutoConfiguration which enables spring boot’s autoconfiguration feature. Since we have added spring-boot-starter-web as a dependency, spring boot will detect it and auto configure the DispatcherServlet class and automatically start the embedded apache tomcat server. @SpringBootApplication is also meta annotated with @SpringBootConfiguration which itself is annotated with @Configuration. So basically this class is also a @Configuration class and you can declare @Bean methods inside this class if you want to create any beans but we are not going to do this in this example. It is also meta annotated with @ComponentScan which will enable spring’s automatic scanning for @Component classes. The automatic scanning feature will detect @Component classes and create beans for them.

Inside the main method we made use of the SpringApplication class provided by spring boot and called it’s static run method. This will cause the spring IOC container to be created and reference to it is stored in the context variable. Since the run method creates the spring IOC container we need to pass a class annotated with both @Configuration and @ComponentScan to it so that we can utilise spring’s annotation based configuraion feature. Since the @SpringBootApplication annotations is meta annotated with these annotations therefore we provide Main.class as an argument to the run method. The run method not only creates the spring IOC container but also runs spring boot’s auto configuration logic which creates the DispatcherServlet and starts the embedded apache tomcat server.

Try running this code in the ide After running the program you will see that the program never stops and will continue running until you stop it by clicking the square button in the ide. You will see an output like this stating that the embedded apache tomcat server has started and is listening on port 8080:-

Open your favourite browser and type in ‘http://localhost:8080' in the address bar. You will be shown the default error page.

This means that our spring boot web app is working correctly and responding to our request. Stop the web app by pressing the square button. Now lets create one more more class which is the same class called DemoController which we used in the Spring section of this article.

DemoController.java:-

package org.example;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/hello")
    String getRequestHandler() {
        return "hello from spring web mvc web app";
    }

}

The explanation for this class is exactly the same as mentioned in the Spring secton of this article. The only thing that's different here is that we are using an embeded apache tomcat servlet container instead of a separate apache tomcat servlet container installed on our computer.

Now run the program again and this time type in ‘http://localhost:8080/hello’ in the address bar of your browser. You should see the string ‘hello from spring web mvc web app’ in the browser window.

We have successfully created a spring boot web app that contains an embedded apache tomcat server. We can create a executable jar file for this web app by opening the intellj maven side bar by clicking on the ‘m’ symbol on the right, then clicking on ‘execute maven goal’ and then typing in ‘mvn clean package’. The jar file will be generated inside the target folder with the name ‘spring-boot-web-app-1.0-SNAPSHOT.jar’ and the jar can be run using the ‘java -jar spring-boot-web-app-1.0-SNAPSHOT.jar’ command from the terminal.

Conclusion

In this article we have gone through the evolution of java web application development from servlet based web apps to spring web mvc web apps to spring boot web apps and saw how the web application development process became easier as time went on. Thank you for reading this article!.

1
Subscribe to my newsletter

Read articles from Aditya Rao C directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Rao C
Aditya Rao C