Skip to content

Should You Learn Servlets Before Spring Boot? A Practical Guide

When I started learning Java web development, I kept seeing conflicting advice. Some said “start with servlets, they’re the foundation.” Others said “skip servlets, just learn Spring Boot, nobody writes servlets anymore.”

I wasted time going back and forth. Here’s what I wish someone had told me upfront.

The Short Answer

No, you don’t need to learn servlets before Spring Boot. You can build Spring Boot applications without knowing anything about servlets.

But here’s the thing: understanding servlet fundamentals will make you a significantly better Spring Boot developer. It’s the difference between knowing how to use Spring Boot and knowing why Spring Boot works the way it does.

Why I Was Confused

I saw this advice on Reddit:

“You should start with spring core, servlet, jsp, mvc, then go to hibernate, after that spring boot and spring data jpa”

That sounded like a lot. Months of work before even touching Spring Boot.

Then I saw this:

“But servlets are not used anymore, I saw in a reel”

Which one was right?

The Reality: Servlets Are Not Dead

Here’s what nobody explained to me: Spring Boot is built on top of servlets.

When you write a Spring Boot controller:

@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable String id) {
return userService.findById(id);
}
}

Behind the scenes, Spring Boot is using servlets. The DispatcherServlet is the heart of Spring MVC, and it is a servlet.

HTTP Request
|
v
Servlet Container (Tomcat)
|
v
DispatcherServlet (a servlet!)
|
v
Your Controller
|
v
Response

Every request that hits your Spring Boot application flows through a servlet. Spring Boot just hides the complexity.

What Spring Boot Hides From You

I didn’t appreciate this until I tried writing a simple servlet myself.

The Servlet Way

@WebServlet("/users")
public class UserServlet extends HttpServlet {
private UserService userService = new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String id = req.getParameter("id");
User user = userService.findById(id);
resp.setContentType("application/json");
PrintWriter out = resp.getWriter();
out.print("{\"name\":\"" + user.getName() + "\"}");
out.flush();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
BufferedReader reader = req.getReader();
// Manual JSON parsing...
String name = extractNameFromJson(reader);
User user = userService.create(name);
resp.setStatus(HttpServletResponse.SC_CREATED);
resp.setContentType("application/json");
// Manual JSON response...
}
}

That’s 30+ lines for what Spring Boot does in 5.

The Spring Boot Way

@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/{id}")
public User getUser(@PathVariable String id) {
return userService.findById(id);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User createUser(@RequestBody CreateUserRequest request) {
return userService.create(request.getName());
}
}

Same functionality, but Spring Boot handles:

  • JSON serialization/deserialization
  • Dependency injection
  • URL mapping
  • Error handling
  • Content negotiation

When Missing Servlet Knowledge Hurts

I ran into problems I couldn’t solve because I didn’t understand what was happening under the hood.

Problem 1: Debugging Request Issues

I had a filter that was modifying requests. Things were breaking in weird ways. I couldn’t figure out why.

Once I learned about servlet filter chains, it made sense:

@Component
public class LoggingFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
System.out.println("Request: " + req.getRequestURI());
chain.doFilter(request, response); // Pass to next filter or servlet
}
}

Spring Security is built entirely on servlet filters. Understanding the filter chain helped me debug authentication issues.

Problem 2: Interview Questions

I got this question in an interview:

“How does Spring MVC handle a request internally?”

I stumbled through an answer. The interviewer wanted me to explain that DispatcherServlet receives the request, finds the handler through HandlerMapping, executes it through HandlerAdapter, and returns the response.

Without servlet knowledge, I couldn’t explain what DispatcherServlet even was.

Problem 3: Legacy Code

My first job had me maintaining an old application. It used servlets directly. No Spring. I was lost.

public class LegacyServlet extends HttpServlet {
// I had no idea what init(), service(), destroy() meant
}

The Decision Matrix

After going through this, here’s how I’d advise different learners:

Your GoalLearn Servlets First?Time to Invest
Get a job quicklyNo, learn Spring Boot firstN/A
Deep understandingYes, 1-2 weeksMedium
Maintain legacy appsYes, 2-3 weeksHigh
Pass interviewsYes, concepts onlyLow
Build personal projectsNoN/A
Enterprise Java careerYes, thoroughHigh

Path A: Pragmatic (What I Should Have Done)

Start with Spring Boot, circle back to servlets:

  1. Learn Spring Boot basics (2 weeks)
    • REST controllers
    • Spring Data JPA
    • Auto-configuration
  2. Build 2-3 projects
  3. Learn servlet fundamentals (1 week)
    • HttpServlet lifecycle
    • Request/Response handling
    • What Spring Boot abstracts

This gives you quick wins while building a foundation.

Path B: Comprehensive (For Career-Focused Learners)

If you have time and want depth:

  1. Servlet fundamentals (1 week)
    • Lifecycle: init() -> service() -> destroy()
    • HttpServletRequest and HttpServletResponse
    • Filters and Listeners
    • Session management
  2. Spring Framework Core (1 week)
    • Dependency Injection
    • AOP basics
  3. Spring MVC (1 week)
    • DispatcherServlet deep dive
  4. Spring Boot (2 weeks)
    • Auto-configuration
    • Building production apps

Path C: Interview Prep (Quick Concepts)

If you just need to pass interviews:

  1. Study servlet concepts:
    • Lifecycle methods
    • forward() vs sendRedirect()
    • ServletConfig vs ServletContext
    • Session tracking methods
    • Filter vs Interceptor
  2. Understand Spring MVC flow:
    • HandlerMapping -> HandlerAdapter -> ViewResolver

What Servlets Are Still Used For

Servlets are actively maintained. Jakarta EE 10 includes Servlet 6.0 (released 2022). Spring Boot 3.x uses Servlet 5.0+.

Direct servlet coding is rare, but servlets power:

  • All Spring MVC applications
  • Spring Security (servlet filters)
  • Embedded Tomcat in Spring Boot
  • Many microservices frameworks

Minimum Servlet Knowledge

Even if you skip deep servlet study, learn these basics (1-2 days):

  • Request/Response model - HTTP requests come in, responses go out
  • Servlet lifecycle - init(), service(), destroy()
  • Servlet container - Tomcat manages servlets
  • What web.xml did - Legacy configuration (now replaced by annotations)

For real understanding:

  • Write a basic HttpServlet
  • Implement a Filter
  • Use HttpSession
  • Understand the threading model (each request is a thread)
  • Deploy to Tomcat manually (not through Spring Boot)

What Happens When You Know Servlets

I can now read Spring source code and understand what’s happening:

// Spring's DispatcherServlet (simplified)
public class DispatcherServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
// 1. Find handler mapping
HandlerMethod handler = getHandler(req);
// 2. Execute interceptors
if (!applyPreHandle(req, resp, handler)) {
return;
}
// 3. Invoke controller method
Object result = handler.invokeForRequest(req, resp);
// 4. Render response
render(result, req, resp);
}
}

This is the “magic” of Spring MVC. It’s just a servlet with a lot of orchestration.

Myths I Believed

Myth 1: “Servlets are obsolete”

Wrong. Servlets are the foundation of all Java web frameworks. Spring Boot doesn’t replace servlets; it uses them.

Myth 2: “Nobody writes servlets anymore”

True for direct coding, false for understanding. Modern frameworks generate servlet-based code. If you need to debug or extend them, you need servlet knowledge.

Myth 3: “Spring Boot replaced servlets”

Spring Boot uses servlets extensively. DispatcherServlet is the heart of Spring MVC. Spring Security is built on servlet filters.

The Verdict

Learn servlet concepts, not servlet coding.

You don’t need to write servlets professionally. But understanding them will:

  1. Make you a better Spring Boot developer
  2. Help debug complex issues
  3. Enable you to work with legacy systems
  4. Prepare you for interviews
  5. Build foundational knowledge for advanced topics

My recommendation: Start with Spring Boot for practical skills. Dedicate 1-2 weeks to servlet fundamentals. Revisit servlets whenever you hit Spring Boot “magic” that doesn’t make sense.

One to two weeks of servlet study provides disproportionate long-term value. I wish I had done it earlier.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments