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.
- Right, Click on the project we have previously created in the “creating the dynamic web application project” tutorial.
- Click New.
- Now if the list has the servlet click it if not perform the step 4. Otherwise, skip the step 4 in the image.
- Click on other and search for “Web” Expand it and look for Servlet in it and click next.
Rename servlet and choose its directory
- Write your package name.
- Write your servlet class name, leave other as default.
- Click Next.
URL mapping for servlet in eclipse
- 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.
- If you want to edit the URL mapping, you can click Edit.
- Give your Servlet URL map a name here.
- Click OK.
- Now click Next.
create servlet without doPost.
- Uncheck the doPost as we have no need for it for now.
- Click Finish.
Servlet with getting method in Java
- Now we have servlet Class created and opened in the new tab.
- Notice the following things the annotation WebServlet has a field named urlPattern, this has the value that you have mentioned in step 10 above.
- Our servlet class extends the HttpServlet class. (Assuming you have known the concept of OPPs). httpServlet has methods doGet(), doPost and others.
- 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
- Now we will run and test our servlet. Expand your project in the Navigator tab.
- Expand “src” find your created class in the package under src.
- Right, click on the class.
- Click “RunAs”.
- Now click “Run on Server”.
Choose server to run servlet
- Select the Tomcat server
- 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.
- This URL selection is telling the Project Context in which your servlet resides.
- And the remaining URL at the end is the urlPattern you have named in step 10 through which your servlet should have accessed.
- This message is the result of execution of the goGet() method in our servlet class.
- Open the servlet by double clicking on it.12345response.setContentType("text/html");PrintWriter out= response.getWriter();out.println("<h3>Hello user </h3>");
- 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.
- You can repeat steps 21 to 25 to test your edit; Now we have successfully created our first servlet class.
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.