To create a servlet:
1. Create a new Dynamic Web Project and configure the project name, source, and context root.
2. Right click the project and select New > Servlet to generate a servlet class. Configure the package name, class name, and URL mapping.
3. The URL mapping defines which requests will trigger the servlet and follow the pattern of http://localhost:port/context-root/servlet-name. Multiple mappings can be defined separated by commas.
3. http://gsbprogramming.blogspot.com
In Project Explorer, select your project name and Right Click New Servlet. You can also
select your project and hit ctrl + N to choose servlet option.
4. http://gsbprogramming.blogspot.com
Enter Package name (if you want) and class name. You can also change source folder. Once
done click Next.
Enter the description. You can add/edit the URL mapping. Once the URL pattern matches
this mapping, your servlet will be called using the type of call (get/ post). Below mapping will
corresponds to following URL pattern:
http://localhost:<port>/<context –root>/ServletClass1
5. http://gsbprogramming.blogspot.com
Choose the method doGet and click finish.
Following class will be generated:
Your servlet will now be visible under Java Resources src package name in Project
Explorer and also in Navigator
6. http://gsbprogramming.blogspot.com
Right click your servlet and choose Run As Run on Server.
Following page will be shown:
Here is the URL pattern:
http://localhost:8081/ServletDemo1/ServletClass1
ServletDemo1 is context root.
/ServletClass1 in pattern
For example, if you want to have URL pattern that starts with ServletClass and followed by any
sequence of character, then you can add URL pattern like:
/ServletClass*
* is wildcard here.
8. http://gsbprogramming.blogspot.com
So, you can add multiple URL mapping by just separating them with comma:
urlPatterns = { urlMapping1, urlMapping2, urlMapping3 }
Once you right click the servlet class and run it, it will display the context root (/ServletDemo1). To
change the Servlet Context Right click the Project (ServletDemo1) and click Properties. Then in Web
Project Setting change the Context Root.