- REST is a software architecture “style”;
- Servlet is a server-side technology.
You can, for example, implement REST-like services using Servlets.
- Servlets are Java specific but RESTful web services are not.
- Servlets are API but RESTful is not.
- RESTful web service can use Servlets as there implementation but vice versa is not true.
- Servlets can run in Servlet container only but RESTful services can run in web container as well.
- 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.
- Request handling in both is totally different e.g. Servlets are multi-threaded inherently but RESTful services are not.
- 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