31

I need to create 5 methods on the server side, which will work with binary data. The remote clients are applet and JavaScript. The Client will send files to the server, and the server must parse these files and then return the response as XML/JSON.

So I am confused - is it good practice to use REST-service in this case? Or should I use a servlet?

My colleague told me:

"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"

However, I see some disadvantages with using Servlet: I need to send a function name that I want to call (i.e. as extra HTTP parameter send function name) and then inside the doPost method perform the following switch:

switch(functionName) {
 case "function1":
   function1(); 
   break;
 case "function2"
   function2(); 
   break;
//.... more `case` statements....

}

In case of REST I can simple use different URLs for different functions. Also, in the case of REST, it more convenient to return JSON/XML from server.

Draken
  • 3,134
  • 13
  • 34
  • 54
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • 3
    Your colleague probably doesn't understand what REST is. It's an architecture style as mentioned in one of the answers here. Statements like "REST is slower than Servlet; REST must be created only when it will be used by many apps" are blantantly wrong. – asgs Sep 21 '12 at 07:38
  • @asgs sorry, I talk about RESTful (web-services. about using concrete implementation of REST - `Jersey` library). So it is not right that RESTful web-service is slower than Servlet? – WelcomeTo Sep 21 '12 at 07:58
  • 1
    To Know more about REST service Refer this link [Rest - Service](http://www.mkyong.com/tutorials/jax-rs-tutorials/) – Nagaraj Sep 21 '12 at 07:25
  • 1
    I did a lot research, built demo apps, trained others & wrote lengthy notes, if you starting from scratch see my github notes here, for a full overview: https://github.com/manoharreddyporeddy/my-programming-language-notes/blob/master/my-java/src/web-services/README.MD – Manohar Reddy Poreddy Aug 29 '18 at 03:46

7 Answers7

48

You are confusing two paradigms here:

  • REST is a software architecture “style”;
  • Servlet is a server-side technology.

You can, for example, implement REST-like services using Servlets.

Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
  • yes, I understand differences. I know that REST build on top of Servlets. But I talk about concrete technologies (`HTTPServlet` and some REST implementation (i.e. `Jersey`)). – WelcomeTo Sep 21 '12 at 07:14
  • 3
    In case of using servlets to implement REST style services, I need to create exactly ONE servlet for every URL I want. But when using REST-service (Jersey) I can create only ONE class – WelcomeTo Sep 21 '12 at 07:16
  • Then I suggest you update your question to reflect this, as this is quite unclear. – Sébastien Le Callonnec Sep 21 '12 at 07:16
  • ok, will update question. So what you think about my question? It's right to create REST-service? Or is better to create servlet in this case? – WelcomeTo Sep 21 '12 at 07:22
  • Is it possible to replace `Servlets` by `RestFul Webservices` in HTML web pages ? From the performance point of view, which one is better? – Hosein Aqajani Sep 19 '20 at 06:19
29

Well, I wouldn't agree with your colleagues' opinion that isn't good to have rest used by only one application, since you may decide in the future to have different applications using the same rest api. If I were you I would choose the pure REST. Why?

  1. If you're using some framework for rest implementation (let's say apache cxf or jersey) you get lots of stuff out of the box - you write POJO you get a rest, you get serialization and deserialization, to and from let's say, JSON object out of the box (eventually you will need to implement some JsonProviders but this is not a big deal).

  2. It is intuitive to work (if you design your rest APIs well).

  3. Very easily consumable by JavaScript clients (especially if you're using JQuery or something like that)

However, it strongly depends of what exactly do you want to do, if you have some strong transactional logic, the rest could be quite tricky. If you're only going to do POST requests (without using the other HTTP methods) you might want to use the Servlet since you won't have to work with additional frameworks and making more dependencies. Note that the REST is more or less an architectural concept and it does not contradict with the Servlet technology, if you're stubborn enough you can make a rest api only with servlets :-). Hope I've helped.

Cyril Gavrailov
  • 466
  • 3
  • 6
  • +1 for mentioning "if you're only going to do POST requests (without using the other HTTP methods) you might want to use the Servlet (like JSON over RPC) since you won't have to work with additional frameworks (like Jersey or Apache CXF) and making more dependencies." – sactiw May 20 '15 at 18:23
5

First, you're speaking in terms of two different paradigms. It's kinda apples-and-oranges.

REST is a style of service that uses HTTP operations (GET, PUT, etc.) to read and write the state of resources. Think of resources as "nouns" and "things".

Servlet, on the other hand, is a software specification originally provided by Sun Microsystems for connecting HTTP requests to custom Java code. Servlets often speak in terms of method calls: "verbs" and "actions".

Since your question implies that you are looking to deal with input->output methods, just a plain servlet should do the job.

mopolopo
  • 55
  • 1
  • 3
3

I cannot see any problems on using Jersey and create a REST service. I know that I'm a REST-taliban but it's really simple to implement this kind of architecture using JAX-RS, so... why not?

Your colleagues say: "REST must be created only when it will be used by many apps" but I cannot see how this can be true.Why I cannot create a REST service for a single app?

Enrichman
  • 11,157
  • 11
  • 67
  • 101
2

Sounds like your collegue is applying premature optimisation. If you can write it with a JAX-RS library quickly, do so... If it then proves to be the bottleneck only then do you take the time to rewrite as servlet.

In my experience the performance overhead of JAX-RS is not sufficiently big to justify the development and maintenance overhead of writing the equivalent in servlets directly where the problem maps well to JAX-RS

Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63
1

Depending on your container version, Jersey (or any other JAX-RS implementation) will still use a Servlet for dispatching requests to the appropriate handler.

If your application is truly RESTful, then JAX-RS will do what you want. Otherwise, consider using a FrontController to interpret the Request and forward it to the appropriate handler.

Also, don't confuse XML or JSON with REST. You will get these for free in most (if not all) JAX-RS implementations, but these implementations still delegate content marshalling to other libraries (e.g. JAXB).

David Grant
  • 13,929
  • 3
  • 57
  • 63
  • yes, I use WAS 7, that don't support JEE 6. And I will use not true JAX_RS, I will use `Jersey` implementation. So you suggest me to use servlet in this case? I understand that Jersey build on top of Servlet. But I think using REST is more convenient in case of many methods? – WelcomeTo Sep 21 '12 at 07:28
  • Jersey is the reference implementation for JAX-RS. I don't know if your application is RESTful or not. Have you considered Spring MVC? – David Grant Sep 21 '12 at 07:29
  • I can't use Spring (it decision of my boss, so I can't do anything), only Java EE. `I don't know if your application is RESTful or not` - don't understand what you mean. I confused what to use - RESTful or Servlet. – WelcomeTo Sep 21 '12 at 07:36
  • As Sébastien Le Callonnec states, REST is an architectural style. It sounds like you're confusing REST with anything that goes over HTTP. – David Grant Sep 21 '12 at 07:39
  • Sorry, I talk about RESTful (web-service), not about REST style. SO what me prefer on WAS 7 (don't support JAX-RS natively) - FrontController or RESTful? – WelcomeTo Sep 21 '12 at 07:56
  • RESTful describes an application that conforms to the REST style. I can't tell you if you application is RESTful from the question. If you're changing what you do based on a parameter, REST is not for you and I would recommend writing a simple front controller. – David Grant Sep 21 '12 at 07:58
1

Here is the similiar link. he has done it with simple servlet http://software.danielwatrous.com/restful-java-servlet/

hello
  • 11
  • 1