- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
Open your NetBeans IDE and click on the red circled icon shown in the following figure to create new java project −
Select JavaWeb then Web Application and click on next.
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.
Select the server and leave the rest as it is. Then click on next.
Leave everything as it is and click on next.
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.
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.
Now, go to source packages folder and right click on default package. Then click on new and click Servlet to create a new servlet.
Provide servlet class name and click next. We have given ‘Servlet1’.
In this step, Give the name to your servlet and URL followed by ‘/’. Leave the rest as it is and click on finish.
You can see the ‘Servlet1.java’ file inside default package.
Now, go to your index.html file and change your code with the given code.
<!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>
Moving ahead, right click on ‘FistServlet’ project name on your left and then click on Run.
You will see the following result on your browser.
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.
Kickstart Your Career
Get certified by completing the courseGet Started