servlet vs RESTful web service

  • REST is a software architecture “style”;
  • Servlet is a server-side technology.

You can, for example, implement REST-like services using Servlets.

  1.  Servlets are Java specific but RESTful web services are not.
  2. Servlets are API but RESTful is not.
  3. RESTful web service can use Servlets as there implementation but vice versa is not true.
  4. Servlets can run in Servlet container only but RESTful services can run in web container as well.
  5. RESTful talks about Resources/Entities/Verbs and gives you more Specific way to use the services i.e There is clear explanation on the usage of HTTP verbs.
  6. Request handling in both is totally different e.g. Servlets are multi-threaded inherently but RESTful services are not.
  7. If you want to share the RESTful services with other teams/client/public to consume there are plenty of Document tools which can generate documentation for your REST services (e.g swagger) but you won’t find that kind of documentation tools with Servlets.

 

There are two flavours of webservices 1) SOAP based 2) RESTful webservices.

Difference between servlets and soap based webservices: i) these webservices can’t be directly accessed through browser. ii) We need not to write webservices prototype separate, which is generating wsdl(which describes webservices using xsd), it can be published to consumers of webservices iii) soap ws are introduced to easily support SOA(Service Oriented Architecture)

Difference between servlets and restful webservices: i) Even though restful webservices directly works on http/https protocal, it provides some other web methods in addition to get, post are put, delete. Which allows to write db operation based structured code easily. ii) As restful webservices allows for pathparameters apart from query parameters, we can easily write class level and method level convenient operations-code. iii) we can use postman like browser plugins to easily check rest-api’s. We can easily add headers, body parameter, selecting different methods, content type along with request to required restful webservice under an URL. iv) we can also provide security to rest-webservices easily through header section of http request msg’s

 

 

Reference :

Atul Tiwari, Big data and Analytics (2016-present)
Gowtham