Reputation: 7677
When writing web services using Jersey I am adding a ServletContainer to the web.xml
<servlet>
<servlet-name>Back-End API</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
What is the use of this ServletContainer? isn't tomcat the ServletContainer?
Is there a way to implement Rest web services without Jersey or any other third party framework?
Upvotes: 0
Views: 1086
Reputation: 1109072
What is the use of this ServletContainer?
Usually the use of any class is explained in its javadoc. Fortunately the Jersey guys took javadocs seriously and your answer is indeed right there.
com.sun.jersey.spi.container.servlet
Class ServletContainer
A
Servlet
orFilter
for deploying root resource classes.If this class is declared as a filter and the initialization parameter
PROPERTY_WEB_PAGE_CONTENT_REGEX
is not set orFEATURE_FILTER_FORWARD_ON_404
is not set to true then the filter must be declared at the last position in the filter chain as the filter will not forward any request to a next filter (if any) in the chain.The following sections make reference to initialization parameters. Unless otherwise specified the initialization parameters apply to both server and filter initialization parameters.
The servlet or filter may be configured to have an initialization parameter "com.sun.jersey.config.property.resourceConfigClass" or "javax.ws.rs.Application" and whose value is a fully qualified name of a class that implements
ResourceConfig
orApplication
. If the concrete class has a constructor that takes a single parameter of the type Map then the class is instantiated with that constructor and an instance of Map that contains all the initialization parameters is passed as the parameter. Otherwise, the class is instantiated as a singleton component managed by the runtime, and injection may be performed (the artifacts that may be injected are limited to injectable providers registered when the servlet or filter is configured).If the initialization parameter "com.sun.jersey.config.property.resourceConfigClass" or "javax.ws.rs.Application" is not present and a initialization parameter "com.sun.jersey.config.property.packages" is present (see
PackagesResourceConfig.PROPERTY_PACKAGES
) a new instance ofPackagesResourceConfig
is created. The initialization parameter "com.sun.jersey.config.property.packages" MUST be set to provide one or more package names. Each package name MUST be separated by ';'. The package names are added as a property value to a Map instance using the property name "com.sun.jersey.config.property.packages". Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor ofPackagesResourceConfig
.If none of the above resource configuration related initialization parameters are present a new instance of
WebAppResourceConfig
is created. The initialization parameter "com.sun.jersey.config.property.classpath" MAY be set to provide one or more resource paths. Each path MUST be separated by ';'. The resource paths are added as a property value to a Map instance using the property name "com.sun.jersey.config.property.classpath". Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor ofWebAppResourceConfig
. If the initialization parameter is not present then the following resource paths are utilized: "/WEB-INF/lib" and "/WEB-INF/classes".All initialization parameters are added as properties of the created
ResourceConfig
.A new
WebApplication
instance will be created and configured such that the following classes may be injected onto a root resource, provider and Application classes using Context:HttpServletRequest
,HttpServletResponse
,ServletContext
,ServletConfig
andWebConfig
. If this class is used as aServlet
then theServletConfig
class may be injected. If this class is used as aFilter
then theFilterConfig
class may be injected.WebConfig
may be injected to abstract servlet or filter deployment.A
IoCComponentProviderFactory
instance may be registered by extending this class and overriding the methodinitiate(ResourceConfig, WebApplication)
to initiate theWebApplication
with theIoCComponentProviderFactory
instance.
isn't tomcat the ServletContainer?
Yup, it is. It's just that the name of the class com.sun.jersey.spi.container.servlet.ServletContainer
is badly chosen. They'd better have named it RestController
or RestApiDispatcher
orso.
Is there a way to implement Rest web services without Jersey or any other third party framework?
Yup. You have several options:
Upvotes: 3