10

I was going through a demo project setup for Restful webservice using Apache CXF, where I happened to come by a piece of code inside web.xml:

    <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

I did not really understand the use of a servlet class in this web.xml. I googled for org.apache.cxf.transport.servlet.CXFServlet and found:

The CXFServlet class, which is defined by Apache CXF, is generated and registered to handle incoming requests.

Now, I really do not understand what that line means

  1. Does this servlet pose as a front-controller, like in Spring MVC flow?
  2. What is the actual purpose of using this servlet class?
  3. How does CXF use Spring to provide XML configuration of services defined in the project?
  4. Does org.glassfish.jersey.servlet.ServletContainer serve the same purpose in Jersey Implementation as org.apache.cxf.transport.servlet.CXFServlet with Apache CXF?

Help me clarify these questions.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
New2Java
  • 273
  • 1
  • 4
  • 19
  • Please restrict yourself to one question per question, asking multiple questions make your question too broad, and make it hard to give targeted and complete answers. Especially your points 3 and 4 should really be separate questions. – Mark Rotteveel Aug 07 '18 at 12:03
  • @MarkRotteveel Will keep in my mind from next time onwards...Thanks for suggestions. – New2Java Aug 08 '18 at 04:34

1 Answers1

11

The JAX-RS specification is built on top of the Servlet specification. Each implementation should have a Servlet as an entry point to the application. When a request comes in, it gets processed by that Servlet. CXFServlet is CXF's implementation of that entry point Servlet.

Does this servlet pose as a front-controller, like in Spring MVC flow?

Pretty much. It's analogous to Spring MVC's DispatcherServlet

What is the actual purpose of using this servlet class?

As mentioned above, it's the entry point to the JAX-RS (CXF) application.

How does CXF use Spring to provide XML configuration of services defined in the project?

It uses Spring to wire up components; connect all of them together. But it's not required (see also).

Does org.glassfish.jersey.servlet.ServletContainer serve the same purpose in Jersey Implementation as org.apache.cxf.transport.servlet.CXFServlet with Apache CXF?

Pretty much.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks @PaulSamsotha... I guess I can start with these information to dig up more on the same. It really helped me to understand a bit easily. Though I would like to ask one more question here. Can you please elaborate a little the sentence **JAX-RS specification is built on top of the Servlet specification**. That would help me. – New2Java Aug 08 '18 at 04:41
  • 1
    [JAX-RS is a specification](http://download.oracle.com/javaee-archive/jax-rs-spec.java.net/jsr339-experts/att-3593/spec.pdf). In this document, they specify that a JAX-RS application should be run as a servlet application in a servlet container. – Paul Samsotha Aug 08 '18 at 04:59