Difference between servlet and web service
来源:互联网 发布:程序员专用计算器在线 编辑:程序博客网 时间:2024/05/16 07:25
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.
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 beingJersey), 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.
Regarding the second question: The @Controller
annotation is a stereotype annotation that tells spring something about what your bean is supposed to do. What exactly this controller will return depends on the actual implementation of your methods, you can configure spring to return plain text, HTML, JSON, XML 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.
- Difference between servlet and web service
- The Difference Between “Add Web Reference” and “Add Service Reference”
- Difference between Android Service,Thread,IntentService and AsyncTask
- Difference between applicationContext.xml and spring-servlet.xml in Spring
- What is the difference between GenericServlet, HttpServlet and a Servlet?
- Difference between / and /* in servlet mapping url pattern
- The difference between web container and web server
- The difference between (()) and ().
- difference between "<" and "<<"
- the difference between ClientID and ID of web control
- The difference between java application library and java web lib
- Java WEB: what's the difference between redirect and dispatch?
- SOAP Binding: Difference between Document and RPC Style Web Services
- Difference between HTML control and Web Server control
- Difference Between typedef and #define
- Difference between PCDATA and CDATA
- Difference between CComPtr and CComQIPtr
- Difference between RVCT and GCCE
- 常用meta整理
- 关于"完形崩溃"
- mysql 常见错误
- Hello~
- JavaScript 学习笔记
- Difference between servlet and web service
- JavaScript 错误 - Throw、Try 和 Catch
- 小菜编程成长记系列
- javascript中数组排序方法sort()用法
- 浅谈Oracle 数据库之间数据同步方案
- iBATIS、Hibernate和JPA:哪一款最适合你
- iOS开发UI篇—UITableview控件使用小结
- linux下tomcat查看端口
- 如何利用诸葛IO做数据分析?