JAX-RS specifies a deployment model around Servlets[1]. Why, because in the Java world, that's how web applications are run. Request comes in to a Servlet container (like Tomcat or a container in a full Java EE server), the container hands off the request to the Servlet application, the application processes the request and spits out the response back to the container, which sends it to the client. Spring MVC is a Servlet based framework (main Servlet being DispatcherServlet
). JSF is a Servlet based framework (main Servlet is FacesServlet
). Same way JAX-RS is build around Servlets (main Servlet is implementation specific; in the case of Jersey it's ServletContainer
).
Request comes in to Tomcat, it looks up the servlet mappings, it finds ServletContainer
matches the request URL, it wraps the request in a HttpServletRequest
and ships it off to the Jersey Servlet. Now Jersey can do with it as it pleases, which is a whole lot of processing; such as processing the request to match your methods[2], deserializing entity bodies, and a whole bunch of other stuff that makes all the magic possible. When it's done processing, it send the response back to the container.
why jersey using a servlet?
I think that is made clear above.
JAX-RS not using a servlet?
Not really sure I really understand what you're asking by this, but JAX-RS specifies other deployment models, but a Servlet environment is the only one with any specific requirement details. Other deployment options, like in an SE environment will be implementation specific[1].
Why using JAX-RS, while we can use a just Servlet
You're basically asking, "Why should I use JAX-RS, when I can implement my own REST framework?". Generally, when there is a framework available, use it. If you feel you can make a better one, then do it.
[1] - See 2.3 Publication
[2] - See 3.7 Matching Requests to Resource Methods
UPDATE
So part of the confusion on part of the OP, was that the tutorial he was going through, didn't specify the a Servlet in a web.xml file, which made the OP think that a "vanilla JAX-RS" (which doesn't exist) was being used, ant not an implementation.
JAX-RS is just a specification, and cannot run without an implementation. Yes there is a javax.ws.rx-api.jar
or a javaee-api.jar
that has the classes/interfaces/annotations to compile a JAX-RS application, but there is no actual "engine" in this jar. The actual JAX-RS "engine" is in the specific implementation jars.
I haven't watched the complete tutorial, but I assume that it uses one of the above jars, which led the OP to believe that no JAX-RS implementation is used. But in actuality, the Java EE server used (which is Glassfish), internally has the implementation. In the case of Glassfish it's Jersey.
Another point of confusion may have been in the app configuration. Instead of using a web.xml as in the OP's post, there is an Application
subclass being used. Something like
@ApplicationPath("/rest")
public class AppConfig extends Application {
...
}
The JAX-RS specification states that when this class with annotation is available, a Servlet should be created with the above fully qualified class name as the Servlet name, and the url mapping the be the value in the @ApplicationPath
. So whatever implementation you are using, this behavior should be the same.