Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Restful web service v Servlets

Options
  • 29-01-2015 12:14am
    #1
    Registered Users Posts: 2,183 ✭✭✭


    Hey,

    just wondering if anyone can clear something up for me... im playing around with JAX-RS (Jersey) and doing some tutorials on restful web services..
    Having only limited knowledge of Servlets, are restful web services seen as a better/neater solution to servlets?... i.e. no middleman as such intercepting http requests and passing off to business functions?..
    or should i be using servlets in certain circumstances for things it can do that web services cant?


Comments

  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    I prefer using REST nowadays - much clearer separation of UI and backend, and facilitates creation of mobile apps retrieving data from the backend. Often I'll use servlets to provide the service, although I'm moving more to node.js. That said, Jersey gives you really good control of the service although it isn't very widespread. If you're trying to identify the difference, I suppose Jersey is really a special kind of servlet.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    I've been out of the loop when it comes to Java for years now but I had the impression a servlet was just a server side technology. Where as a RESTful web services is a web application with a particular architecture intended to be consumed by other applications. So you could build a RESTful service using servlets if you wanted to. So you're not comparing like with like if you use a direct comparison.


  • Registered Users Posts: 2,183 ✭✭✭jobless


    bpmurray wrote: »
    I prefer using REST nowadays - much clearer separation of UI and backend, and facilitates creation of mobile apps retrieving data from the backend. Often I'll use servlets to provide the service, although I'm moving more to node.js. That said, Jersey gives you really good control of the service although it isn't very widespread. If you're trying to identify the difference, I suppose Jersey is really a special kind of servlet.

    When you say 'user servlets to provide the service' .... is the rest service not the service?


  • Registered Users Posts: 2,183 ✭✭✭jobless


    Aswerty wrote: »
    I've been out of the loop when it comes to Java for years now but I had the impression a servlet was just a server side technology. Where as a RESTful web services is a web application with a particular architecture intended to be consumed by other applications. So you could build a RESTful service using servlets if you wanted to. So you're not comparing like with like if you use a direct comparison.

    Yeah i understand they shouldnt be directly compared, i guess im just wondering when you would use servlets anymore when rest web services can be used instead that implement JAX-RS?.... seems like an extra layer...
    Is there anything extra servlets give us?


  • Registered Users Posts: 586 ✭✭✭Aswerty


    As I said before Java isn't my area of expertise but from a general perspective on web applications the following is true.

    A web application will implement some kind of HTTP framework. Servlets in Java are such a framework. Typically what one of these frameworks will do is things like URL routing, provide an interface with http servers (e.g. Apache), construct and deconstruct HTTP messages, etc.

    A RESTful web service implements a HTTP framework in some way. So how does such a service differ from a typical servlet based web application? The answer is that a RESTful service has a set of characteristics a web application won't necessarily have. These characteristics include:
    • Full statelessness – web applications often have things like user sessions but a session is just a remembered state over a period of time. This shouldn't ever happen in a RESTful system.
    • Fixed API – a web application generally relies on a user navigating to different pages by clicking links after arriving on the home page. A machine consuming a RESTful service calls a specific URL. Therefore with a web app the developer can change the URL for a specific page as long as they update the link on the homepage (or whatever). But in a RESTful service changing the URL will just return a HTTP 404 error to the machine making the request to that specific address. Therefore it is much more important in a web service that the URLs don't change.
    • Data Driven – most web applications serve up HTML along with CSS and JavaScript. While a RESTful service can serve up whatever it wants typically it serves up structured data that other services and applications can consume (e.g. JSON, streams, etc).
    There are loads of other recommended architectural features one should implement for a RESTful service such as caching, layering, etc. Though most of these features are also recommended for large scale web applications.

    Coming back to JAX-RS vs Servlets. If we see that a servlet can be used in both web applications and RESTful service then what is the point of JAX-RS? Essentially JAX-RS is a platform that implements some kind of servlet functionality but it also brings features that are well suited to building RESTful services. So essentially it is JAX-RS that is the extra layer on top of servlet like technology.

    As with most things, some other guys have probably said this better: http://stackoverflow.com/questions/7052152/why-use-jax-rs-jersey


  • Advertisement
  • Registered Users Posts: 2,183 ✭✭✭jobless


    Aswerty wrote: »
    As I said before Java isn't my area of expertise but from a general perspective on web applications the following is true.

    A web application will implement some kind of HTTP framework. Servlets in Java are such a framework. Typically what one of these frameworks will do is things like URL routing, provide an interface with http servers (e.g. Apache), construct and deconstruct HTTP messages, etc.

    A RESTful web service implements a HTTP framework in some way. So how does such a service differ from a typical servlet based web application? The answer is that a RESTful service has a set of characteristics a web application won't necessarily have. These characteristics include:
    • Full statelessness – web applications often have things like user sessions but a session is just a remembered state over a period of time. This shouldn't ever happen in a RESTful system.
    • Fixed API – a web application generally relies on a user navigating to different pages by clicking links after arriving on the home page. A machine consuming a RESTful service calls a specific URL. Therefore with a web app the developer can change the URL for a specific page as long as they update the link on the homepage (or whatever). But in a RESTful service changing the URL will just return a HTTP 404 error to the machine making the request to that specific address. Therefore it is much more important in a web service that the URLs don't change.
    • Data Driven – most web applications serve up HTML along with CSS and JavaScript. While a RESTful service can serve up whatever it wants typically it serves up structured data that other services and applications can consume (e.g. JSON, streams, etc).
    There are loads of other recommended architectural features one should implement for a RESTful service such as caching, layering, etc. Though most of these features are also recommended for large scale web applications.

    Coming back to JAX-RS vs Servlets. If we see that a servlet can be used in both web applications and RESTful service then what is the point of JAX-RS? Essentially JAX-RS is a platform that implements some kind of servlet functionality but it also brings features that are well suited to building RESTful services. So essentially it is JAX-RS that is the extra layer on top of servlet like technology.

    As with most things, some other guys have probably said this better: http://stackoverflow.com/questions/7052152/why-use-jax-rs-jersey

    thanks aswerty, it does clear some of if up..... ill take a read of that link!


  • Registered Users Posts: 50 ✭✭EamonnDunne


    Spring MVC allows you to setup a REST endpoint very easily with a few annotations, its very easy to use.


Advertisement