Johann
Johann

Reputation: 29877

Difference between @Path and @WebServlet

In Java EE I notice that you can specify a path to a uri either as

@Path("/SomePath")
public class MyClass

or

@WebServlet("/SomePath")
public class MyClass extends HttpServlet

I think @Path is used for non-servlet stuff while @WebServlet is used for servlets. But do they effectively serve the same purpose?

Info on @Path can be found here: http://docs.oracle.com/cd/E19798-01/821-1841/6nmq2cp26/index.html

But at first glance, it seems to provide some of the basic functionality as @WebServlet.

Upvotes: 6

Views: 6092

Answers (2)

Mahder
Mahder

Reputation: 419

The @Path annotation identifies the URI path template to which the resource responds and is specified at the class or method level of a resource. The @Path annotation’s value is a partial URI path template relative to the base URI of the server on which the resource is deployed, the context root of the application, and the URL pattern to which the JAX-RS runtime responds.

The @WebServlet annotation is used to declare a servlet. The annotated class must extend the javax.servlet.http.HttpServlet class.

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27516

@Path annotation defines a path to a RESTful Web service so when you have @Path("/SomeService") it will translate into www.yourapp.com/baseRestUrl/SomeService. You can also define it on the methods which provides REST services. Note that baseRestUrl is defined inside web.xml or in class which extends Application class.

On the other hand @WebServlet("/SomePath") states that Servlet will be listening for request on the www.yourapp.com/SomePath, it is basically replacement of servlet-mapping element in web.xml. You can still configure servlets like this, it's up to you whether you prefer XML or annotation configuration.

Upvotes: 10

Related Questions