[英]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.

假設我使用@Controller注釋創建spring mvc web app注釋幾個類,並創建一些將成功從前端傳輸一些信息的東西 - >后端,反之亦然,也許某些數據庫可能涉及到后端端。

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


7 个解决方案



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 resonses. 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 3.1

另一方面,servlet用於傳輸HTTP請求和共振。它可用於編寫帶有JSP和HTML的Web應用程序,或用於提供XML和JSON響應(如在RESTful服務中),當然也可用於接收和返回SOAP消息。您可以將其視為Web服務下的一層。 Servlet有自己的標准,目前是Java Servlet規范3.1版

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 afaik that is not the main purpose of this framework and Jersey is considerably easier to use.

要使用servlet技術編寫Web服務,您可以使用例如JAX-WS(例如,用於SOAP)。為了編寫RESTful服務,你可以使用JAX-RS(參考實現是Jersey),或者你也可以使用Spring WebMVC,但是afaik並不是這個框架的主要目的,而且Jersey更容易使用。

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.

旁邊的注釋,一個用@Controller注釋的類還不是一個servlet,它只是一個bean。您如何使用servlet主要取決於您使用的框架。例如,當您使用Spring時,Servlet作業由Springs DispatcherServlet完成,后者又將請求轉發給正確的bean。如果您使用Tomcat,那么您可以直接編寫自己的servlet,只需子類化javax.servlet.http.HttpServlet類並覆蓋必要的方法,例如doGet,它響應瀏覽器的HTTP GET請求。



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.

servlet是一種特定於Java的編寫軟件的方式,可以響應HTTP請求。 Spring MVC抽象了很多實現細節,使編寫Web應用程序變得更容易,但是使用了servlet。



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定義了更高級別的抽象,例如某些特定於業務的功能。而Servlet只是一個負責數據傳輸的軟件實現組件。

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.

Web Service實現通常依賴於servlet來接收數據。但是,它也可以使用它處理協議數據的自定義層。

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




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.




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

Web服務的運行級別高於servlet。 Servlet是API,它很簡單,並提供編寫服務器端組件的功能。

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

例如,RESTfull是一個Web服務,它包含許多其他“功能”以及servlet。要部署,我們可以將web.xml定義為 -


which is none but a servlet




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.

Servlet和Web Service之間最明顯的區別是:您通過HTTP訪問servlet,同時通過SOAP(簡單對象訪問協議)訪問Web Service。但是,實際上,您不能直接調用servlet,如果調用者不在您的應用程序中,您只能打開URL連接並將一些參數放入servlet。並且您不能限制調用者可以放置的參數。調用者不知道servlet可以接收哪些參數。因此,您最好使用Web服務為其他應用程序提供API,Web服務的WSDL文件可以為調用者提供足夠的信息來調用您的Web服務。



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.

Web Service使用ServletContainer類,它又是一個Servlet類,它以干凈和結構化的方式處理請求。 REST代表REpresentational STateless Protocol。這里請求不會存儲任何數據。

The REST Web Service supports HTTP methods

REST Web服務支持HTTP方法

  1. GET - Usually to fetch data.
  2. GET - 通常用於獲取數據。
  3. POST - To insert new Object.
  4. POST - 要插入新對象。
  5. PUT - To update the existing Object.
  6. PUT - 更新現有對象。
  7. DELETE -To delete the Object.
  8. 刪除 - 刪除對象。

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

我們可以將任意數量的URL映射到Web Service類,它可以具有任何類型的HTTP方法。

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.




粤ICP备14056181号  © 2014-2021