WebServlet annotation is introduced in Servlet 3.0, it's used to simplify the servlet creation (we don't need to define it and its URL mapping in web.xml)
The WebServlet annotation usage is
1 |
@WebServlet(URL) |
e.g.
1 2 3 |
@WebServlet("/servlet/GetMailFileOwner") public class GetMailFileOwner extends HttpServlet { ... |
Then the user can visit /servlet/GetMailFileOwner to access this servlet
But today I met a problem with using WebServlet annotation. First I added WebServlet on the Linux machine (Eclipse Luna), and switched to another Windows machine (MyEclipse), then I tried to rebuild the project, I got following error:
1 |
WebServlet cannot be resolved to a type |
On the Windows machine, I use the default servlet.jar which is added when I create the new Dynamic Web Project. But I replaced the default servlet.jar file with Tomcat 8.0 libraries.
So the solution is to add Tomcat 8.0 libraries to Java Build Path (We need first download Tomcat 8.0 and set it up for Eclipse, otherwise Eclipse will not find this Tomcat 8.0 libraries)