servlet和Web服務之間的區別

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

你會怎么稱呼它?休息Web服務或servlet或其他什么?

7 个解决方案

#1


74  

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:

Web服務是一種服務,它使用REST編程范例或SOAP協議為其客戶端提供服務方法。有幾種方法可以實現Web服務。編寫Web服務最簡單的方法是編寫一個類,並使用來自javax.jws的@WebService和@WebMethod注釋對其進行注釋,然后使用以下命令從main方法啟動它:

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.

結果是您可以在注冊的URL上查看WSDL,如果您有SoapUI或任何其他SOAP客戶端,您還可以測試和使用您的Web服務。

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.

更全面和實用的方法是使用框架編寫Web服務並將其發布到應用程序服務器或servlet容器(如Tomcat或JBoss)上。在這種情況下,您將使用Servlet來處理傳輸SOAP或REST消息的HTTP請求的傳輸。

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.

關於第二個問題:@Controller注釋是一個Spring特定的構造型注釋,告訴Spring一些關於你的bean應該做什么的東西。控制器的確切方法將返回取決於方法的實際實現,您可以將Spring配置為返回純文本,HTML,JSON,XML,二進制數據或任何您想要的內容。

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請求。

#2


35  

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

您所描述的是一個Web應用程序,人類使用瀏覽器與軟件系統進行交互。

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

Web服務是軟件系統使用HTTP和XML或JSON相互通信的一種方式,無需任何人員參與。

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。

#3


10  

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.

@Controller可能與servlet更相關,而servlet也是實現傳輸的一種方式。

#4


2  

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

servlet是一個HTTP查詢處理程序。您可以使用傳入的查詢執行所需操作。在JVM上運行的servlet。

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.

Web服務與一個或多或少嚴格的協議相關聯:接口(API)定義了可用的方法及其參數和服務的返回值。

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.

使用協議機制公開此接口。這些協議與將運行服務的主機無關:您可以使用PHP,Java,C#或您自己的語言定義相同的Web服務。您只需要擁有一段能夠理解協議查詢並能夠生成客戶端可讀答案的代碼。

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

例如,SOAP是一種Web服務協議:Wikipedia定義:

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.

SOAP,最初定義為簡單對象訪問協議,是用於在計算機網絡中的Web服務的實現中交換結構化信息的協議規范。

#5


2  

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定義為 -

<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

這不過是一個servlet

#6


2  

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服務。

#7


2  

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.

另一方面,每個servlet只能有1個URL映射。盡管可以在請求參數條件的幫助下實現最終要求,但是現在使用servlet不會提供干凈的方法。

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

在webservice中,我們可以在Class級別和Method級別定義URL路徑,這允許我們以更結構化的方式進行編碼。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/05/08/725ddb7ca8408c2f40d20010ed6e33c1.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com