Difference between servlet and web service

JavaWeb ServicesSpringRestTerminology

Java Problem Overview


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 ?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

What you're describing is a web application, where a human uses a browser to interact with a software system.

A web service is a way for software systems to communicate with each other using HTTP and XML or JSON, without any humans involved.

A servlet is a Java-specific way of writing software that responds to HTTP requests. Spring MVC abstracts away a lot of the implementation detail to make writing web applications easier, but uses servlets under the covers.

Solution 3 - Java

My take on it would be that Web Service defines higher level abstraction such as some business specific functionality. While Servlet is just a software implementation component responsible for transport of data.

Web Service implementation would typically rely on servlet for receiving data. However, it can as well use it's custom layer of dealing with protocol data.

@Controller is probably more related to Web Service than servlet which is,again, a way to implement transport.

Solution 4 - Java

The most obvious difference between Servlet and Web Service is: You access servlet via HTTP while access Web Service via SOAP (Simple Object Access Protocol). But, in fact, you can not directly invoke a servlet, you can only open URL connection and put some parameter to the servlet if the caller is out of your application. And you can not restrict what parameters the caller can put. The caller does not know what parameters your servlet can receive either. So, You'd better use web service to provide API to other applications, the WSDL file of your web service can give the caller enough information to invoke your web service.

Solution 5 - Java

A servlet is an HTTP query handler. You can do what you want with your incoming queries. A servlet run on the JVM.

A web service is tied to a more or less rigid protocol: An interface (API) is defined with available methods and their arguments and return values for the service.

This interface is exposed using the protocol mechanisms. These protocols are agnostic about the host that will run the service: you can define the same web service using PHP, Java, C# or your own language. You only need to have a piece of code able to understand queries for the protocol and able to produce answers readable by the client.

For example SOAP is a web service protocol: Wikipedia definition:

> SOAP, originally defined as Simple > Object Access Protocol, is a protocol > specification for exchanging > structured information in the > implementation of Web Services in > computer networks.

Solution 6 - Java

Web services operate on a higher level than servlets. Servlets are API which is simple and provides capabilities to write server side components.

For example RESTfull is a Web Service which contains many other "functionality" along with servlet. To deploy, we may define the web.xml as -

<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
	<param-name>com.sun.jersey.config.property.packages</param-name>
	<param-value>jersey.rest.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

which is none but a servlet

Solution 7 - Java

Web Service uses ServletContainer class which is again a Servlet class, which handles the request in clean and structured way. The REST stands for REpresentational STateless Protocol. Here the request won't store any data.

The REST Web Service supports HTTP methods

  1. GET - Usually to fetch data.
  2. POST - To insert new Object.
  3. PUT - To update the existing Object.
  4. DELETE -To delete the Object.

We can map any number of URLs to Web Service class which can have any type of HTTP methods.

On other hand, there can be only 1 URL mapping can be done for each servlet. Though the end requirement can be achieved with the help of request parameter conditions, but using servlet nowadays won't provide clean way.

In webservice we can define URL path at Class level as well as Method level, which allows us to code in more structured way.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGandalf StormCrowView Question on Stackoverflow
Solution 1 - JavalanoxxView Answer on Stackoverflow
Solution 2 - JavaartbristolView Answer on Stackoverflow
Solution 3 - JavaAlex GitelmanView Answer on Stackoverflow
Solution 4 - JavaChinniView Answer on Stackoverflow
Solution 5 - JavaGuillaumeView Answer on Stackoverflow
Solution 6 - JavadgmView Answer on Stackoverflow
Solution 7 - JavaKranti123View Answer on Stackoverflow