Reputation: 47
I don't exactly know what are the main differences between them and what are the benefits of servlet and controller.
Upvotes: 4
Views: 4728
Reputation: 58822
Controller is part of Model-View-Controller pattern:
Model–View–Controller (usually known as MVC) is an architectural pattern commonly used for developing user interfaces that divides an application into three interconnected parts. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.1 The MVC design pattern decouples these major components allowing for efficient code reuse and parallel development.
Servlet can be a Controller
Controller acts as an interface between View and Model. Controller intercepts all the incoming requests.
Model represents the state of the application i.e. data. It can also have business logic.
But doesn't have to be (can do "model"/"view" operations).
Upvotes: 1
Reputation: 49646
I don't know what is the main difference between them.
A controller is a part of an architectural pattern.
A servlet is a part of a server (usually, a web container).
What are the benefits of servlet.
To answer this, you need to understand what they were primarily designed for. Basically, their main purpose is to respond to requests in a dynamic and independent (from other servlets) way.
They can be stateful.
They can manage sessions.
They can communicate with other servlets.
They are easily portable.
The lifecycle of a servlet is usually managed by a container which makes things way easier.
The question is a bit vague and it's hard to answer it concisely. It's better for you to get some hands-on experience working with both. Prior to doing so, pore over the materials I mentioned below.
https://en.wikipedia.org/wiki/Java_servlet
(!) https://en.wikipedia.org/wiki/MVC
When to use Servlet or @Controller
(!) https://javaee.github.io/servlet-spec/downloads/servlet-4.0/servlet-4_0_FINAL.pdf (see Overview)
Upvotes: 4