I suggest you doing it using Netbeans.
STEP 1:
File -> New Project -> Select Java With Maven -> Select Web Application
Name and Location: You give the project name (this becomes your context path by default)
Settings > Choose Server: choose the server you want to use to run your servlets (Apache Tomcat, GlassFish, etc...)
Click Finish
Now NetBeans generates a basic webapp for you.
STEP 2:
Then, you need to add the servlet :
Right-Click on your project name -> New -> Servlet
Enter Class Name
URL Pattern: /"yourURLPattern"
Click Finish
Netbeans should create the class and register it via @webServlet or web.xml
On the body of the Servlet method, since Tomcat 10+ migrated from javax.servlet to jakarta.servlet, if your code uses javax.*, pick Tomcat 9 or GlassFish. But if you want to keep using Tomcat 10/11, change imports from javax. * to jakarta. *.
STEP 3:
Right-click Project name → Run.
NetBeans will start the server, deploy the app, and open your browser.
Your servlet URL will be:
http://localhost:8080/yourProjectName/URLPattern
If it opens your browser in a port different from 8080, you have to change it accordingly.