Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Servlet or @Controller

I need to get a few things cleared up. I have been looking for an answer for this one, but I can't seem to find a good answer to my specific questions (eg. this question was nibbling on the answer: Difference between servlet and web service).

To my understanding, there are different ways you can implement the "request handling", aka "Controller", in an "MVC oriented" web application, two of them being:

  1. A Java specific Servlet (ie. one you create by clicking new -> Servlet, in eclipse for example), used as a "Controller". This one extends HttpServlet and you use methods like doGet and doPost etc.
  2. A Spring MVC annotated @Controller class (yes, using a DispatcherServlet). With this one you use the @RequestMethod GET/POST etc.

Now to my questions...

  • When do you use one or the other?
  • Are there any general advantages to use one method over the other? (Like, is one method recommended over the other in general?)

[EDIT]: Emphasized keywords

like image 291
Roger Avatar asked May 08 '13 11:05

Roger


People also ask

What is the difference between controller and servlet?

A controller is a part of an architectural pattern. A servlet is a part of a server (usually, a web container).

Why do we use servlet as controller instead of JSP?

So Servlet is used as controller. JSP is again a servlet which has syntax more like to HTML with java support. Whatever user is going to see as result of his request is HTML. Programmer can easily write html tags inside JSP to render html.

Which is better servlet or Spring boot?

Servlets are based upon a low-level API for handling requests and responses. Web frameworks like Spring MVC are designed to make building web applications, which handle HTTP requests and responses, easier. Most Java web frameworks, including Spring MVC, use servlets behind the scenes.

Should I use JSP or servlet?

Servlet should be used when there is more data processing involved whereas, JSP is generally used when there is less involvement of data processing. Servlets run faster than JSP, on the other hand JSP runs slower than servlet as it takes time to compile the program and convert into servlets.


1 Answers

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.

like image 63
Ben Thurley Avatar answered Sep 24 '22 13:09

Ben Thurley