Create a new Servlet in Java

How to Create a Servlet in Java:

In the previous tutorial, we have learned about creating the dynamic web application project. In this tutorial, we will learn about how to create a servlet in web application project. So what is a servlet? How can we create it using Eclipse IDE?

The servlet is a Java program that runs on the server, in our case the server is Tomcat, Tomcat has a container whose responsibility is to handle requests and response coming from the client, container collaborates with the servlet to handle dynamic responses and requests,  servlet does not have the main method, it has some default methods that can be executed when you access the servlet.

Let’s start creating a servlet.

Create a new servlet
Create a new servlet
  1. Right, Click on the project we have previously created in the “creating the dynamic web application project” tutorial.
  2. Click New.
  3. Now if the list has the servlet click it if not perform the step 4. Otherwise, skip the step 4 in the image.
  4. Click on other and search for “Web” Expand it and look for Servlet in it and click next.Rename servlet and choose its directory
    Rename servlet and choose its directory
  5. Write your package name.
  6. Write your servlet class name, leave other as default.
  7. Click Next.URL mapping for servlet in eclipse
    URL mapping for servlet in eclipse
  8. Now in this window, it is asking for URL mappings. You can leave it as it is Or you can edit it. URL mapping is the string through which this servlet would be accessed.
  9. If you want to edit the URL mapping, you can click Edit.
  10. Give your Servlet URL map a name here.
  11. Click OK.
  12. Now click Next.create servlet without doPost
    create servlet without doPost.
  13. Uncheck the doPost as we have no need for it for now.
  14. Click Finish.Servlet with get method in Java
    Servlet with getting method in Java
  15. Now we have servlet Class created and opened in the new tab.
  16. Notice the following things the annotation WebServlet has a field named urlPattern, this has the value that you have mentioned in step 10 above.
  17. Our servlet class extends the HttpServlet class. (Assuming you have known the concept of OPPs). httpServlet has methods doGet(), doPost and others.
  18. Now here we are providing our implementation of one of the methods of HttpServlet Class and the method is doGet().Run the servlet on the server
    Run the servlet on the server
  19. Now we will run and test our servlet. Expand your project in the Navigator tab.
  20. Expand “src” find your created class in the package under src.
  21. Right, click on the class.
  22. Click “RunAs”.
  23. Now click “Run on Server”.Choose server to run servlet
    Choose server to run servlet
  24. Select the Tomcat server
  25. Click finish.
    25.1. It will run our servlet class on the server, notice the URL, this part of URL “http://localhost:8080/” we have discussed this part previously.
    running servlet
    Running servlet
  26. This URL selection is telling the Project Context in which your servlet resides.
  27. And the remaining URL at the end is the urlPattern you have named in step 10 through which your servlet should have accessed.
  28. This message is the result of execution of the goGet() method in our servlet class.
  29. Open the servlet by double clicking on it.
  30. And edit the do get method as given in source 1.9 Explanation: in the doGet() methods we have two more parameters named request and response. In the given source1.9 we are using the response object to send output to the client in the form of HTML. We can do much more with this request and response objects we would explain them later.
  31. You can repeat steps 21 to 25 to test your edit; Now we have successfully created our first servlet class.

Download Complete Servlets Project Here

In this tutorial we have accomplished following things, we got to know how to create the servlet class, run it on the server and examine its output coming from the doGet Method of our Servlet. We have also edited the doGet method and got to know about the response object. In the next tutorial, we will learn more in detail about servlets.