Intro
Building a Simple Web App with Spring MVC
Securing our Spring MVC Web App
Session Management
Finishing Up

What is Spring Web?

Web Applications in Modern Software

  • Web applications refer to programs which users and inner components interact with via web communication (typically HTTP).
    • For example, for common full-stack CRUD applications, this involves letting the user interact with the app via webpages, which then send HTTP requests to, and receive HTTP responses from, a so-called “back-end application server” (typically an API), so that data can be processed in a way that gets persisted in a database.
  • Web apps can be found in various domains, including e-commerce, social media, online banking, content management, and more.
  • The presentation layer of web apps can easily be ported to native mobile apps and native desktop apps.
  • The ubiquity of the web and ease of portability make it the most popular choice for developing full-stack enterprise applications.
  • As such, building robust, secure, and maintainable web applications is a priority for developers.
Illustration of a server-side rendered full-stack web application, where a back-end application server (in this case a Java/Spring application) also bears the presentation templating responsibility, generating the webpages internally with data it can fetch itself inside the same application, typically backed by a database, in this case a MariaDB RDBMS, before sending it to the client as a response.
Illustration of a client-side rendered full-stack web application, where a JS framework (in this case Angular) bears the presentation templating responsibility, and dynamically fills in the templates using data fetched from various back-end REST APIs (in this case a Java/Spring application), typically backed by a database, in this case a MariaDB RDBMS.

Web Applications in the Different Programming Languages

Depending on which programming language you chose, you’ll most likely start building the back end for web applications using one of the following popular frameworks:

Programming LanguageBack-End Web Frameworks
JavaScriptNode.js, Express.js
PythonDjango, Flask, Pyramid
RubyRuby on Rails
JavaSpring Boot, Java EE
PHPLaravel, Symfony, Zend Framework
C#ASP.NET Core
GoGin, Beego
RustRocket
KotlinKtor
ScalaPlay Framework

If Java is your programming language of choice, you’ll probably be building web apps using the Spring ecosystem.

The Spring Ecosystem

Welcome to the world of Spring Web, a popular part of the Spring Framework ecosystem!

  • Spring Boot offers various Spring starter projects that allow us to get up and running as quickly as possible.
  • These all build on top of Spring Boot, so we expect a solid foundation and certain comfort in Spring Core and Spring Boot.
Illustration of various Spring Boot starter projects that are “plug-and-play” i.e. easy to start using or swap out in our Spring Boot applications.

What is Spring Web

  • Spring Web itself is Spring’s fundamental web module that abstracts and simplifies the usage of Servlets in our IoC container, allowing for HTTP client and web-related functionality.
    • Various modules build on top of Spring Web to support web application building, e.g.:
      • Spring Web MVC
      • Spring REST
      • Spring WebFlux
      • Spring WebSocket
      • Spring Security Web
    • The core Spring Web module is easy to install as a Maven dependency.
    • Spring Web provides a rich set of tools for testing and mocking, making it easier to write unit and integration tests for web components.
  • Spring Web is most famous for its support of the Model-View-Controller (MVC) design pattern, encapsulated in the Spring Web MVC module, which you will learn more about in the next topic.
  • Spring Web is the core, non-boot, library, but we often informally use the name “Spring Web” also when referring to the Spring Boot Starter Web project, which is different and includes a lot more than just Spring Web.

What is Spring Boot Starter Web

  • Spring Boot Starter Web gets us up and running with Spring Web even faster by working with Spring Boot to autoconfigure everything for us, as well as by including an embedded Tomcat container.
    • Includes:
      • Spring Web
      • Spring Web MVC
      • Embedded Tomcat servlet container
        • This lets us run our webapps without having to externally install and run a Tomcat server.
      • & more!
Screenshot of the MVN Repository page for Spring Boot Starter Web, highlighting the inclusion of Spring Web and Spring Web MVC. Please note that the screenshotted versions may not be the newest anymore.
  • We will be using Spring Boot Starter Web for the rest of this course, taking full advantage of the included Spring Web, Spring MVC and embedded Tomcat container, to get up & running as quickly as possible.

Servlets: A Recap of the Foundation of Java on the Web

Servlets are the backbone of Java web applications, making HTTP request/response processing possible, but what are they exactly?

Click here to reveal a quick summary on servlets
  • Concretely, servlets are just classes we write that extend the HttpServlet-class.
  • Servlets contain logic to receive HTTP requests from clients (e.g., web browsers) and generate corresponding HTTP responses.
  • Servlets are configured and deployed within a servlet container, which are runtime environments that enable and manage the execution of all the servlets inside it.
    • Popular examples include Tomcat and Jetty.
    • They handle all the super technical, low-level HTTP communication aspects of the operation, including request dispatching and multithreading, so developers can focus on writing servlets with the business logic they want.
    • They manage each servlet’s instantiation, initialisation and destruction.
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.IOException;

public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Handle GET requests here
        String message = "Hello, World!";
        response.getWriter().write(message);
    }
}

In this example, MyServlet is a simple servlet that responds with “Hello, World!” when accessed via a web browser.

  • In summary, servlets are how Java applications can process HTTP requests/responses, making it possible for web clients to communicate with our app.
  • They require to be configured and deployed in servlet containers like Tomcat, which abstract away all the super nitty-gritty HTTP communication details.
  • It’s clear, then, why Spring Web would build on top of this Servlets API, simplifying web development even further, and why Spring Boot Starter Web provides an embedded Tomcat for us to be able to spin up a working web server than can handle servlets, without needing to install such a container/webserver separately.
  • Creating and working with custom servlets is outside the scope of this course, as we’ll be relying on Spring Web’s further abstractions to simplify the process and make our lives easier when it comes to developing and maintaining our codebase.

Summary

  • Web applications enable user interaction through web communication, making them versatile and widely used in various domains.
  • Choice of programming language dictates the back-end web frameworks used to build web applications. In the case of Java, it’s Java EE or Spring.
  • The Spring Framework ecosystem simplifies Java application development, with Spring Web being a core module for web app development.
  • Servlets are classes used to handle HTTP request/response processing in Java web applications, and they play a crucial role in back-end web development.
  • Servlets run inside and are managed by Servlet containers (like Tomcat), which take care of all the super low-level, nitty gritty HTTP communication details so we can focus on our business logic more.
  • Spring Web builds on top of the Servlets API, simplifying web development and providing higher-level abstractions.
  • Spring Boot Starter Web is a Spring Boot project starter that includes Spring Web, Spring Web MVC and an embedded Tomcat container so we can get up an running faster with building web apps.