Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between servlet and web service

What is the difference between these 2? I found few results on google nothing conclusive.

Here is a follow up question:

Say I create spring mvc web app annotate couple of classes with @Controller annotation and create something that will successfully transfer some information from front end -> back end and vice versa and perhaps some database might be involved on the back end side.

What would you call that? Rest web service or servlet or something else ?

like image 442
Gandalf StormCrow Avatar asked May 08 '11 22:05

Gandalf StormCrow


People also ask

What is servlet application?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

What is the difference between application server and servlet container?

In Layman terms : A web Server means: Handling HTTP requests (usually from browsers). A Servlet Container (e.g. Tomcat) means: It can handle servlets & JSP. An Application Server (e.g. GlassFish) means: *It can manage Java EE applications (usually both servlet/JSP and EJBs).

Is REST API A servlet?

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.

What servlet is called when a REST API request is made?

An HttpServlet is a natural, convenient way to implement RESTful web services for two main reasons.


1 Answers

A web service is a service that provides service methods to its clients using either the REST programming paradigm or the SOAP protocol for communication. There are several ways to implement a web service. The most simple way to write a web service would be to write a class and annotate it with the @WebService and @WebMethod annotations from javax.jws, and then launch it from a main-method with:

Endpoint.publish("http://localhost:8089/myservice", new MyWebService()); 

The result is that you can view the WSDL at the registered URL and if you have SoapUI or any other SOAP client you can also test and use your web service.

A servlet on the other hand is used to transport HTTP requests and responses. It can be used to write a web application with JSPs and HTML, or to serve XML and JSON responses (as in a RESTful service) and of course also to receive and return SOAP messages. You can think of it as one layer below web services. Servlets have their own standard which is currently the Java Servlet Specification Version 4.0

A more comprehensive and practical approach is to write a web service with a framework and to publish it on an application server or servlet container such as Tomcat or JBoss. In this case you would use a Servlet to handle the transport of the HTTP requests which transmit your SOAP or REST messages.

To write a web service with servlet technology you can for example use JAX-WS (e.g. for SOAP). In order to write RESTful services, you can either use JAX-RS (with the reference implementation being Jersey), or alternatively you can use Spring WebMVC, but as far as I know that is not the main purpose of this framework and Jersey is considerably easier to use.

Regarding the second question: The @Controller annotation is a Spring specific stereotype annotation that tells Spring something about what your bean is supposed to do. What exactly a method of a controller will return depends on the actual implementation of your methods, you can configure Spring to return plain text, HTML, JSON, XML, binary data or what ever you want.

A note on the side, a class that is annotated with @Controller is not yet a servlet, it is simply a bean. How you use servlets depends mainly on the Framework that you use. For example, when you use Spring, the servlet job is done by Springs DispatcherServlet which in turn forwards requests to the correct beans. If you use Tomcat, then you can directly write your own servlets by simply subclassing the javax.servlet.http.HttpServlet class and overwriting the necessary methods such as doGet which responds to HTTP GET requests from your browser.

like image 62
lanoxx Avatar answered Oct 13 '22 16:10

lanoxx