Skip to main content

Command Palette

Search for a command to run...

🔍 What I Wish I Learned About Servlets and JSP Before Diving Into Spring Framework

Updated
•5 min read
🔍 What I Wish I Learned About Servlets and JSP Before Diving Into Spring Framework
A

I’m a Computer Science undergrad from India with a passion for coding and building things that make an impact. Skilled in Java, Data Structures and Algorithms (DSA), and web development, I love diving into problem-solving challenges and constantly learning. Right now, I’m focused on sharpening my DSA skills and expanding my expertise in Java development.

When I began learning the Spring Framework, especially Spring MVC, I thought I could skip the older technologies like Servlets and JSP, assuming Spring was modern and there was no need to spend time on legacy Java web tech. That mindset caused me problems because Spring was built on top of Servlets and JSP, and understanding those basics would have made learning Spring much easier. If you're just starting with Spring like I did, this article covers everything I wish someone had explained to me before I got overwhelmed by annotations and auto-config magic.


đź§  What Spring Is Really Doing Behind the Scenes

You write this in Spring:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, world!";
    }
}

Simple, right? But what you’re not seeing is what’s happening under the hood:

  • Spring sets up a DispatcherServlet (which is just a fancy HttpServlet)

  • It routes your /hello request to HelloController

  • It handles the HTTP request, builds the response, and returns it

That whole process is built on the Servlet API.

So even if you never touch a raw HttpServlet, your Spring app is full of them — just abstracted.


đź§± So What Is a Servlet, Exactly?

A Servlet is a Java class that runs inside a servlet container (like Tomcat) and responds to HTTP requests.

Here’s what a simple one looks like:

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().write("Hello from Servlet!");
    }
}

This is the low-level version of what Spring’s @RestController is doing for you.

Once I understood that, suddenly:

  • @RequestMapping made sense

  • HttpServletRequest and HttpServletResponse weren’t scary

  • The whole Spring MVC flow felt less “magical”


🔄 Servlet Lifecycle: Why You Should Know It

Here’s how Servlets live and die:

  1. init() → Called once when the servlet is loaded

  2. doGet() / doPost() → Called for each incoming request

  3. destroy() → Called before the servlet is taken down

Knowing this helped me understand:

  • When Spring beans are created and destroyed

  • How @PostConstruct and @PreDestroy relate to servlet lifecycle

  • Why stateless components are safer for multi-threaded web apps


🚦 DispatcherServlet: Spring’s Heartbeat

Spring MVC is built around one central servlet: DispatcherServlet.

Here’s what it does:

  1. Intercepts all HTTP requests (/api, /home, etc.)

  2. Decides which controller should handle the request

  3. Passes the request to the right controller method

  4. Returns the response (JSON, HTML, etc.)

It’s literally configured as a Servlet in Spring Boot’s auto-configuration.

Understanding this servlet-based routing system helped me troubleshoot:

  • Why some URLs weren’t being matched

  • How Spring MVC processes @RequestParam, @PathVariable, etc.

  • How exception handling works in the request pipeline


đź§ľ Do I Need to Learn JSP?

Short answer: No — unless:

  • You’re maintaining a legacy Java EE app

  • You’re specifically working with JSP views (not common in new Spring Boot apps)

But I still recommend learning basic JSP syntax, because:

  • Many older Spring tutorials still use JSP

  • You’ll understand how the view layer evolved

  • You’ll appreciate why modern Spring uses Thymeleaf instead

Quick JSP Overview:

<%@ page contentType="text/html" %>
<html>
<body>
  <h1>Hello, <%= request.getParameter("name") %></h1>
</body>
</html>

JSP lets you embed Java into HTML — not ideal for clean code, which is why modern apps use template engines like Thymeleaf or full frontend frameworks (React, Angular, etc.).


đź§  Putting It All Together: Spring MVC Flow with Servlet Context

Here’s how a Spring MVC request is handled:

  1. User sends a request: GET /user/10

  2. Tomcat receives it and passes it to Spring’s DispatcherServlet

  3. DispatcherServlet consults HandlerMapping to find the right controller

  4. Spring calls your method: @GetMapping("/user/{id}")

  5. The method returns data (JSON, HTML, etc.)

  6. DispatcherServlet sends the response back to the browser

If you know how Servlets process requests, this whole flow is super intuitive.


âś… Checklist: What Servlet/JSP Knowledge You Actually Need Before Spring

ConceptRequired?Why It’s Useful
What is HTTP (GET, POST, headers)âś… YesCore web concepts
What is a Servlet?âś… YesSpring runs on top of them
Servlet lifecycle (init, doGet)âś… YesMatches Spring bean and request handling
JSP basics (<%= ... %>)🔄 OptionalFor legacy understanding
DispatcherServletâś… YesCentral to Spring MVC
HttpServletRequest / Responseâś… YesYou'll still use these in Spring sometimes
web.xml deployment descriptor❌ NoSpring Boot auto-configures everything

In a nutshell 🥜

If you're starting with Spring or Spring MVC, here's how much Servlet and JSP knowledge you actually need:

  • âś… Understand what a Servlet is — a Java class that handles HTTP requests.

  • âś… Know the Servlet lifecycle — init(), doGet(), doPost(), destroy().

  • âś… Get what DispatcherServlet does — it's the front controller in Spring MVC.

  • âś… Recognize HttpServletRequest and HttpServletResponse — Spring still uses them under the hood.

  • 🔄 JSP is optional — good to know for legacy projects or understanding older tutorials, but most modern Spring apps use Thymeleaf or APIs instead.

  • ❌ You don’t need to touch web.xml or build a full JSP/Servlet app — Spring Boot automates that away.

Spending just a few hours on Servlet basics will massively reduce confusion when you jump into Spring — I wish I did it earlier.

S

Nice 👍

1
F

Really insightful post. Knowing the Servlet basics early would’ve saved me a lot of Spring confusion too — this hits the mark.

1
A

❤️

E

Great write-up! This breaks down the "magic" behind Spring in a way that's clear and beginner-friendly. Understanding Servlets and the DispatcherServlet upfront really does demystify Spring MVC — especially when debugging or customizing behavior. Thanks for highlighting why even legacy concepts still matter today.

1
A

thanks i like you find it helpful.

More from this blog

A

Arka Shares: Documenting my Programming Journey.

27 posts

Here i am documenting my learning journy .