Reputation: 227
I have fundamental idea about servlet and spring mvc. But I don't know that there is a usage of servlets in spring mvc or not. In spring mvc we have controller classes. My thinking is servlet is used in spring mvc as a controller. If I'm incorrect please correct me.
Upvotes: 11
Views: 21087
Reputation: 965
Related / Duplicated to When to use Servlet or @Controller. The question is not the same but quite the explanation on that question you will be able to understand:
If you're a student interested in learning the language then I would stick with servlets for now. It's possible to write a web app using just servlets but in practice you'll probably want to look at JSP's too.
A JSP is a convenient way to write a servlet that allows you to mix html with scripting elements (although it's recommended to avoid Java code in your jsp in favour of tags and el expressions). Under the covers it will be compiled as a servlet but it avoids you having to use lots of messy print statements.
It's important to have at least a basic understanding of servlets and JSP's. Spring MVC is one of many frameworks built on top of servlets to try make the task of writing a web application a bit easier. Basically all requests are mapped to the DispatcherServlet which acts as a front controller.
The DispatcherServlet will then call the controller whose annotations match the incoming request. This is neater than having to write these mappings yourself in the web.xml (although with servlet 3.0 you can annotate servlets now). But you also get many other benefits that can be used like mapping form fields to an object, validating that object with jsr303 annotations, map inputs and outputs to xml or json etc etc. Plus it's tightly integrated with core spring so you can easily wire in your services for the controller to call.
It's worth noting that there are a plethora of competing frameworks built on top of servlets. Spring MVC is one of the most popular so it's not a bad choice to look into.
Upvotes: 6
Reputation: 143
Controllers are not Servlets! Controllers are normal Spring MVC beans that do not extend HttpServlet
. Instead what Spring has is a custom extension of HttpServlet
called DispacherServlet
. Looking at the DispacherServlet
's source code you can see that the class hierarchy goes: DispatcherServlet extends FrameworkServlet
→ FrameworkServlet extends HttpServletBean
→ HttpServletBean extends HttpServlet
.
The DispatcherServlet
, like any other Servlet, is declared in the web.xml. It handles all incoming HTTP requests. It is called a front controller which provides a single point of entry in your application. It is responsible for request handling by delegating requests to additional components of Spring MVC controllers which do not extend the HTTP Servlet API.
In this picture DispacherServlet
is the only HttpServlet
. The Controller
s, HandlerMapping
and ViewResolver
are all Spring MVC beans.
Upvotes: 7
Reputation: 146
Yes,you are perfectly right. Servlets are used in Spring-MVC. In Spring-MVC when you write annotation like @Controller, indirectly you are using a Servlet called Dispatcher Servlet. Dispatcher Servlet is defined in web.xml file with properties and class name which is mapped to .jsp pages and Controller part.
Upvotes: 13