Servlet 和 Web 服务之间的区别
这2个有什么区别?我在谷歌上发现了一些结果,没有什么结论性的。
这是一个后续问题:
假设我创建 spring mvc Web 应用程序,使用 @Controller 注释对几个类进行注释,并创建一些可以成功从前端传输一些信息的内容 ->后端,反之亦然,也许后端可能涉及一些数据库。
你会怎么称呼它?休息 Web 服务或 servlet 还是其他什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Web 服务是一种使用 REST 编程范例或 SOAP 协议进行通信的客户端提供服务方法的服务。有多种方法可以实现 Web 服务。编写 Web 服务最简单的方法是编写一个类并使用
javax.jws
中的@WebService
和@WebMethod
注释对其进行注释>,然后从main
方法启动它:结果是,您可以在注册的 URL 处查看 WSDL,并且如果您有 SoapUI 或任何其他 SOAP 客户端您还可以测试和使用您的网络 服务。
另一方面,servlet 用于传输 HTTP 请求和响应。它可用于使用 JSP 和 HTML 编写 Web 应用程序,或提供 XML 和 JSON 响应(如在 RESTful 服务中),当然也可用于接收和返回 SOAP 消息。您可以将其视为Web 服务下面的一层。 Servlet 有自己的标准,目前是 Java Servlet 规范版本4.0
更全面、实用的方法是用框架编写Web服务,并将其发布到应用服务器或Servlet容器(例如Tomcat或JBoss)上。在这种情况下,您将使用 Servlet 来处理传输 SOAP 或 REST 消息的 HTTP 请求的传输。
要使用 servlet 技术编写 Web 服务,您可以使用 JAX-WS(例如 SOAP)。为了编写 RESTful 服务,您可以使用 JAX-RS (参考实现为 Jersey< /a>),或者您可以使用 Spring WebMVC,但据我所知,这不是这个框架的主要目的,而且 Jersey 更容易使用。
关于第二个问题:
@Controller
注释是 Spring 特定的构造型注释,它告诉 Spring 您的 Bean 应该做什么。控制器的方法到底会返回什么取决于方法的实际实现,您可以将 Spring 配置为返回纯文本、HTML、JSON、XML、二进制数据或您想要的任何数据。需要注意的是,用
@Controller
注解的类还不是 servlet,它只是一个 bean。如何使用 servlet 主要取决于您使用的框架。例如,当您使用 Spring 时,servlet 工作由 SpringsDispatcherServlet
完成,后者又将请求转发到正确的 bean。如果您使用 Tomcat,那么您可以直接编写自己的 servlet,只需对 javax.servlet.http.HttpServlet 类进行子类化并覆盖必要的方法,例如响应来自浏览器的 HTTP GET 请求。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 fromjavax.jws
, and then launch it from amain
-method with: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 SpringsDispatcherServlet
which in turn forwards requests to the correct beans. If you use Tomcat, then you can directly write your own servlets by simply subclassing thejavax.servlet.http.HttpServlet
class and overwriting the necessary methods such asdoGet
which responds to HTTP GET requests from your browser.您所描述的是一个网络应用程序,人们在其中使用浏览器与软件系统进行交互。
Web 服务是软件系统使用 HTTP 和 XML 或 JSON 相互通信的一种方式,无需任何人工参与。
servlet 是一种特定于 Java 的编写响应 HTTP 请求的软件的方法。 Spring MVC 抽象了许多实现细节,使编写 Web 应用程序变得更容易,但在幕后使用了 servlet。
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.
我的看法是,Web 服务定义了更高级别的抽象,例如一些特定于业务的功能。而Servlet只是一个负责数据传输的软件实现组件。
Web 服务实现通常依赖 servlet 来接收数据。但是,它也可以使用处理协议数据的自定义层。
@Controller 可能与 Web 服务比 servlet 更相关,而 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 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.
Servlet 和 Web Service 最明显的区别是:
您通过 HTTP 访问 servlet,同时通过 SOAP(简单对象)访问 Web 服务
访问协议)。
但是,事实上,你不能直接调用servlet,你只能打开URL
连接并在调用者不在时将一些参数放入 servlet
您的申请。并且您不能限制调用者可以使用哪些参数
放。调用者不知道您的 servlet 可以接收哪些参数
任何一个。
所以,你最好使用Web服务为其他应用程序提供API,
Web 服务的 WSDL 文件可以为调用者提供足够的信息
调用您的网络服务。
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 是一个 HTTP 查询处理程序。您可以对传入的查询执行您想要的操作。 servlet 在 JVM 上运行。
Web 服务或多或少与严格的协议相关联:接口 (API) 使用可用的方法及其参数和服务的返回值进行定义。
该接口是使用协议机制公开的。这些协议与运行服务的主机无关:您可以使用 PHP、Java、C# 或您自己的语言定义相同的 Web 服务。您只需要一段代码能够理解协议的查询并能够生成客户端可读的答案。
例如 SOAP 是一个 Web 服务协议:
维基百科定义:
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:
Web 服务在比 servlet 更高的级别上运行。 Servlet 是简单的 API,提供编写服务器端组件的功能。
例如,RESTfull 是一个 Web 服务,除了 servlet 之外,还包含许多其他“功能”。
为了部署,我们可以将 web.xml 定义为 -
它只是一个 servlet
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 -
which is none but a servlet
Web 服务使用 ServletContainer 类,它也是一个 Servlet 类,它以干净且结构化的方式处理请求。
REST 代表代表性无状态协议。这里的请求不会存储任何数据。
REST Web 服务支持 HTTP 方法
我们可以将任意数量的 URL 映射到 Web 服务类,该类可以具有任意类型的 HTTP 方法。
另一方面,每个 servlet 只能完成 1 个 URL 映射。
虽然最终的要求可以通过请求参数条件来实现,但是现在使用servlet并不能提供干净的方式。
在Web服务中,我们可以在类级别以及方法级别定义URL路径,这使我们能够以更结构化的方式进行编码。
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
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.