Starting with first Servlet Application


Servlets are small java modules that are used on server side of a web connection to enhance the functionality of web server. All the methods and classes for creating a servlet are available in ‘javax.servlet’ and ‘javax.servlet.http’ packages. Hence, it is important to import them into your program before working with the servlet.

This article will guide you step by step to getting started with your first servlet application. Before moving on it is necessary to know how servlet works. Let’s discuss it briefly.

Servlets

The benefits of using servlets are as follows −

  • Just like the java programs it is also platform independent means once a servlet application is created, we can use it on any operating system.

  • All the resources of the servlet on web server are secured by the Java security manager.

  • A servlet can access all the java class libraries.

Servlet Life Cycle

Whenever we search for a particular URL to a web browser. It generates an HTTP request and send it to the server. The server receives this request and retrieves the specific servlet and loads it on the browser. Now, the init() method is invoked by server. When servlet is loaded into the memory for the first time, the init() method is called. Next, service() method of servlet is invoked to process the HTTP request for each client request. At last, destroy() method is called to release the allocated resources.

Step by step guide to your first Servlet Application

Before starting with your first servlet application, make sure you have installed an IDE and a server. You can use any IDE that supports java programming like IntelliJ or NetBeans. Also, you need a server like glassfish or Tomcat to contain the servlet.

For the sake of this article, we will use NetBeans IDE and Apache Tomcat server.

Step 1

Open your NetBeans IDE and click on the red circled icon shown in the following figure to create new java project −

Step 2

Select JavaWeb then Web Application and click on next.

Step 3

When you click on next, the following interface will get opened. Now, give a project name of your choice and click next. We have given it ‘FirstServlet’.

For your reference, you can refer to the figure below.

Step 4

Select the server and leave the rest as it is. Then click on next.

Step 5

Leave everything as it is and click on next.

Step 6

When you click finish your ‘FirstServlet’ project will get created with necessary folders.

You can see these four folders created in your project ‘FirstServlet’. Let’s discuss them one by one.

  • Web Pages − The HTML, CSS and JavaScript files will be stored in this folder.

  • Source Packages − Here in this folder we will create the java and servlet classes.

  • Libraries − It will contain external libraries like MySQL if any.

  • Configuration Files − It is created automatically by NetBeans IDE. Don’t make any changes to them.

Step 7

Now, go to source packages folder and right click on default package. Then click on new and click Servlet to create a new servlet.

Step 8

Provide servlet class name and click next. We have given ‘Servlet1’.

Step 9

In this step, Give the name to your servlet and URL followed by ‘/’. Leave the rest as it is and click on finish.

Step 10

You can see the ‘Servlet1.java’ file inside default package.

Step 11

Now, go to your index.html file and change your code with the given code.

Example

<!DOCTYPE html>
<html>
   <head>
      <title>Tutorials Point</title> 
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
   <body>
      <div>Welcome to Tutorials Point</div>
   </body>
</html>

Step 12

Moving ahead, right click on ‘FistServlet’ project name on your left and then click on Run.

Output

You will see the following result on your browser.

Conclusion

In this article, we have understood the basic concept of servlet and created a servlet that prints a simple message on the screen. We have listed all the important steps that helped new developers to get started with the first servlet application.

Updated on: 08-May-2023

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements