Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet vs RESTful

Today I read about Restful services. Basically what I understand that is Restful webservices will work on HTTP request methods rather than normal webservice will work on SOAP request.

What is the need for Restful services as normal servlet can also work on the HTTP methods?

like image 398
Dilip Avatar asked Oct 24 '11 11:10

Dilip


People also ask

What is difference between servlet and REST API?

Servlets are API which is simple and provides capabilities to write server side components. Rest provides higher level support for writing services which are denoted by using the concept of resource which represents a concept to be addressed while representation indicates the current state of resource.

Is Java Servlet RESTful?

In Java Servlet RESTFul client tutorial, we create a RESTFul client in a Java Servlet with JAX-RS. Java API for RESTful Web Services (JAX-RS) is a Java API specification that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern.

Can we create REST API using servlet?

In a similar way, you can create REST applications using only the Servlet API. However, there are other APIs that are designed to create REST applications.

What is the difference between servlet and RESTful services?

Servlets can run in Servlet container only but RESTful services can run in web container as well. RESTful talks about Resources/Entities/Verbs and gives you more Specific way to use the services i.e There is clear explanation on the usage of HTTP verbs. Request handling in both is totally different e.g. Servlets are multi-thr

How do you implement RESTful services in Java?

The current chapter introduces a tried-and-true way to do RESTful services in Java: the service is implemented as a JSP script, which a web server such as Tomcat or Jetty translates into a servlet, and the servlet then is published with the web server.

What is a Java Servlet?

The main player is the Servlet: A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol. If you want a very simplistic analogy, the Servlet is Java's version of CGI (Common Gateway Interface).

What are the disadvantages of using rest over servlet?

"Creating REST-service that will be used only by one Application isn't good. REST must be created only when it will be used by many apps. And REST has some disadvantages over servlet: REST is slower than servlet; it more difficult to write thread-safe REST than servlet".


1 Answers

RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it's more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters.

Surely you can do this with a plain vanilla servlet, but it would introduce some boilerplate code to gather the path parameters and to generate the desired response. JAX-RS is just a convenient and self-containing API which removes the need for writing all the boilerplate code yourself, resulting in minimal and more self-documenting code.

Assuming that you've a JAXB entity as model as below:

@XmlRootElement public class Data {      @XmlElement     private Long id;      @XmlElement     private String value;      // ...      @Override     public String toString() {         return String.format("Data[id=%d,value=%s]", id, value);     }  } 

And a JAX-RS resource as below:

@Path("data") public class DataResource {      @EJB     private DataService service;      @GET      @Path("text/{id}")     @Produces(MediaType.TEXT_PLAIN)     public String getAsText(@PathParam("id") Long id) {         return String.valueOf(service.find(id));     }      @GET      @Path("xml/{id}")     @Produces(MediaType.APPLICATION_XML)     public Data getAsXml(@PathParam("id") Long id) {         return service.find(id);     }      @GET      @Path("json/{id}")     @Produces(MediaType.APPLICATION_JSON)     public Data getAsJson(@PathParam("id") Long id) {         return service.find(id);     }  } 

Then you'd already get the desired content in proper format by:

  • http://example.com/rest/data/text/123
  • http://example.com/rest/data/xml/123
  • http://example.com/rest/data/json/123

That's it. Try to do the same with a single plain vanilla Servlet :) Please note that SOAP essentially also goes over HTTP. It's basically an extra XML layer over HTTP, not a different network protocol.

See also:

  • REST tutorial
  • JAXB tutorial
  • Jersey tutorial
like image 82
BalusC Avatar answered Oct 03 '22 17:10

BalusC