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

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

问题描述

这两个有什么区别?我发现谷歌的结果很少,没有结论。

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

这是一个跟进问题:

假设我创建了spring mvc web app,用@注释几个类控制器注释并创建一些将成功从前端传输一些信息的东西 - >后端,反之亦然,也许某些数据库可能涉及到后端。

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服务最简单的方法是编写一个类,并使用 @WebService @WebMethod 来自 javax.jws 的注释,然后从 main -method启动它:

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客户端,您也可以测试和使用您的Web服务。

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消息。您可以将其视为 Web服务下面的一层。 Servlet有自己的标准,目前是 Java Servlet规范版本4.0

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 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 ,但afaik并不是此框架的主要目的,而且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 afaik that is not the main purpose of this framework and Jersey is considerably easier to use.

关于第二个问题:
@Controller 注释是一个 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,那么您可以通过简单地继承 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天全站免登陆