servlet 和 Web 服务的区别 [英] Difference between servlet and web service

查看:32
本文介绍了servlet 和 Web 服务的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个有什么区别?我在 google 上发现的结果很少,没有定论.

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

这是一个后续问题:

假设我创建了 spring mvc web 应用程序,用@Controller 注释对几个类进行注释,并创建一些可以成功从前端传输一些信息的东西-> 后端,反之亦然,也许后端侧可能涉及某些数据库.

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.

你会怎么称呼它?休息 Web 服务或 servlet 或其他什么?

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

推荐答案

Web 服务 是一种服务,它使用 REST 编程范式或 SOAP 协议进行通信,为其客户端提供服务方法.有多种方法可以实现 Web 服务.编写 Web 服务的最简单方法是编写一个类并使用 javax.jws 中的 @WebService@WebMethod 批注对其进行批注>,然后从 main 方法启动它:

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());

结果是您可以在注册的 URL 处查看 WSDL,如果您有 SoapUI 或任何其他 SOAP 客户端,您还可以测试和使用您的网络服务.

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.

另一方面,servlet 用于传输HTTP 请求和响应.它可用于编写带有 JSP 和 HTML 的 Web 应用程序,或提供 XML 和 JSON 响应(如在 RESTful 服务中),当然也可用于接收和返回 SOAP 消息.您可以将其视为网络服务下方的一层.Servlet 有自己的标准,目前是 Java Servlet 规范版本4.0

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

更全面和实用的方法是用框架编写一个 Web 服务,然后将其发布到应用程序服务器或 servlet 容器(例如 Tomcat 或 JBoss)上.在这种情况下,您将使用 Servlet 来处理传输 SOAP 或 REST 消息的 HTTP 请求的传输.

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.

要使用 servlet 技术编写 Web 服务,您可以使用 JAX-WS(例如用于 SOAP).为了编写 RESTful 服务,您可以使用 JAX-RS(参考实现为 Jersey),或者你也可以使用 Spring WebMVC,但据我所知,这不是这个框架的主要目的,而且 Jersey 更容易使用.

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.

关于第二个问题:@Controller 批注是一个 Spring 特定的构造型批注,它告诉 Spring 你的 bean 应该做什么.控制器的方法究竟返回什么取决于方法的实际实现,您可以将 Spring 配置为返回纯文本、HTML、JSON、XML、二进制数据或您想要的任何内容.

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注解的类还不是servlet,它只是一个bean.您如何使用 servlet 主要取决于您使用的框架.例如,当您使用 Spring 时,servlet 作业由 Springs DispatcherServlet 完成,后者将请求转发到正确的 bean.如果您使用 Tomcat,那么您可以直接编写自己的 servlet,只需将 javax.servlet.http.HttpServlet 类子类化并覆盖必要的方法,例如响应于的 doGet来自浏览器的 HTTP GET 请求.

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.

这篇关于servlet 和 Web 服务的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆