Servlet Annotation WebServlet Example

The first example is to show Servlet Welcome using Servlet Annotation WebServlet.

For this we have created an index.html page on index.html we have created a link. On clicking on link it will call welcome servlet.

Our project directory structure is as per below

1_expo

index.jsp

Show welcome servlet

Welcome.java

Servlet Welcome

@WebServlet(name = “Welcome”, urlPatterns = {“/welcome”}) here urlPattern maps /welcome to Welcome.java.
<a href=”welcome”>Show welcome servlet</a> call to doGet() method of Welcome.java because anchor tag calls http get method so it is handled by servlets doGet() method.

Servlet Annotation WebServlet Example
Servlet Annotation WebServlet Example

Result

Servlet Annotation WebServlet Example
Servlet Annotation WebServlet Example

Leave a Comment