Reputation: 41
Currently, it seems like my jax-rs services is using with Jersey ServletContainer but running on Tomcat. This baffled me because according to my understanding, Jersey is a server that have more functionality than Tomcat, and don't contain Tomcat, but now, my project is using Jersey's library but running on Tomcat, how could this happen?
Below is my web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Jersey Servlet -->
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>main.java</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Below is my pom.xml(I used Maven):
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- servlet dependencies -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
Upvotes: 1
Views: 5877
Reputation: 311039
Tomcat is a Servlet container. If Jersey has a Servlet, Jersey can run in Tomcat.
Upvotes: 1
Reputation: 14533
Tomcat is a Java web server, which implements several, but not all, Java EE specifications. Another Java web server would be Jetty. They differ from full application servers like Glassfish or JBoss / WildFly in the number of Java EE specifications they implement. The rather minimal Tomcat implements JavaServer Pages and Java Servlets, which is enough for a lot of applications.
Jersey is a Java library for both serving and calling REST (or mainly HTTP, since not everything is REST) APIs. It's build on top of Java EE specifications, so it can be used on any server that implements these specifications, e.g. Tomcat.
In your web.xml
file you can define multiple servlets. What the servlet does is defined by the <servlet-class>
element. You could pass your own implementation on top of the HttpServlet
. In your case, you are using the Jersey servlet, which then manages all requests to the URLs it is mapped to (<servlet-mapping>
). You can now learn to work with Jersey, implemented your desired API behaviour and build a web archive (.war
). This web archive can then be deployed to any web server, that implements the required specifications, e.g. Tomcat. If you start using other Java EE technologies like Enterprise JavaBeans, you need to check which server implementation implements this technology. You could use Glassfish, there would be no difference for Jersey.
EDIT: I forgot to say that Jersey is one possible (the reference) implementation for the JAX-RS specification, like Tomcat is one possible Java Servlet (and others) implementation. Nevertheless, one is a web server and the other is a web service library, so its not possible to compare them or say that one has "more functionality than" the other.
Upvotes: 7