Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 553
Johann Avatar asked Jan 11 '23 05:01

Johann


1 Answers

@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.

like image 153
Petr Mensik Avatar answered Jan 25 '23 22:01

Petr Mensik