Issue
Earlier servlet version (less than 3.0), we used web.xml file as Deployment descriptor: web.xml
<web-app>
<servlet>
<servlet-name>WebServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebServlet</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Now with the help of tomcat 7.0 and servlet v3.0, we use @WebServlet annotation. E.g:
@WebServlet(
name = "AnnotatedServlet",
description = "A sample annotated servlet",
urlPatterns = {"/Login"}
)
public class LoginServlet extends HttpServlet {
// servlet code here...
}
Now my question is which is better to use professionally? Need your honest suggestion. Thanks in advance.
Solution
It depends on your need:
- If you prefer to keep all the configuration stuff in one place, go for
web.xml
; - Otherwise, if you think (as I do), that the XML configuration files become too heavy when we deal with a big number of declarations and configuration items, go for annotations.
I personally prefer annotations, as it belongs the my Java code and I find myself on it and dealing with big XML configuration files (maybe it's not your case here) is boring and takes a lot of time, for me, to get what I am looking for (this is my personal opinion).
On the other hand, the latter doesn't matter, because, when application works fine, no one will ask you what have you chosen for your servlet declaration.
Answered By - jMounir
Answer Checked By - Robin (JavaFixing Admin)