• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do we use annotations in servlets ?

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw some online servlet examples which use annotations by importing javax.servlet.annotation.WebServlet.
The oracle docs don't clearly explain what these annotations do. Can someone please explain this to me ?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using annotations is an alternative to using/configuring servlets in web.xml. web.xml is an optional deployment descriptor in the latest versions of Java EE. The @WebServlet annotation was introduced in Java EE 6 and the Oracle tutorials have a chapter on it http://docs.oracle.com/javaee/6/tutorial/doc/bnafu.html
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations were introduced in JEE5 as an alternative for detailing configuration in xml files. For example, using an annotation of @WebServlet("/show") for a web servlet, is equivalent to having an web.xml file with:

The annotation can also be used for other servlet parameters such as the init-param, load-on-startup, etc. See the WebServlet documentation for more details.
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone for the answers. I appreciate it very much. Btw, which is preferred in the software industry - xml or annotations ?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which is preferred in the software industry - xml or annotations ?

In order to answer this, i will have to critique the question first:
Why do we use X?

This question implies "We use X", whereas in reality only some people use X, while others don't. So there's an assumption in there that is not generally true.

That said, there is certainly a move towards using annotations and away from XML. For example, Struts 1 turned into an XML nightmare, so comparable newer frameworks (like Stripes) started using annotations instead (plus the convention-over-configuration approach, which obviates the need for either).

However, there is something to be said for the XML variety, at least when web.xml is concerned: There's one place where all servlets and filters are declared - no need to search through the entire code base in order to figure out where servlets or filters might exist. Sure, IDEs can take some of that pain away, but IDEs are not always in use when I want that question answered.
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ulf. It makes sense to me now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!