Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Servlet » Steps to Create a Servlet

  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
    MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi Biography
  • Vedaant Madhavan Biography in Hindi, Family, School, Age
    Vedaant Madhavan Biography in Hindi, Family, School, Age Biography
  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • Diwali The Festival of Lights
    Diwali 2022, Indian Festival of Lights essay (दिवाली त्योहार पर निबंध, कहानी) Festival
  • What is Pronoun?
    What is Pronoun with example? Pronoun definition and examples Grammar
  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography
  • Ramayan : Short Story of ramayana in Hindi | Qualities of Rama
    Ramayan : Short Story of ramayana in Hindi | Qualities of Rama Spiritual

Steps to Create a Servlet

Posted on August 31, 2021April 15, 2022 By GeekCer Education No Comments on Steps to Create a Servlet
Steps to Create a Servlet

There are few simple steps to create a servlet. The steps are common to all web servers. You need to install and configure the web server before creating servlet.

Before proceeding to the steps we are assuming that you already have Tomcat server.

Table of Contents

  • Steps to create a Servlet application using Tomcat Server
  • Step 1: Create a Directory Structure for your application
  • Step 2: Create a servlet, write the servlet source code.
  • Step 3: Compile source code.
  • Step 4: Create a deployment descriptor (web.xml).
    • List of web.xml Deployment Descriptor Elements
  • Step 5: Start the server and deploy the project (After 3 and 4 Steps).
  • Step 6: Access the servlet (After completing all Steps).
  • What is welcome-file-list in web.xml?
  • Load on startup <load-on-startup> in web.xml
    • The key points to remember about <load-on-startup> :

Steps to create a Servlet application using Tomcat Server

Following are the six steps to create a servlet:

  • Create a directory structure for your application.
  • Create a servlet, write the servlet source code.
  • Compile source code.
  • Create a deployment descriptor (web.xml).
  • Start the server and deploy the project.
  • Access the servlet (You can call your servlet from a web browser).

Step 1: Create a Directory Structure for your application

When you install Tomcat Server, the webapps subdirectory is automatically available to you.
Project Directory

  • Create a Directory name called ServletExample.
  • And then Create src\com\geekcer directory inside the ServletExample.
  • Create WEB-INF\lib directory inside the ServletExample put the Servlet jar file into the lib directory.
  • Create WEB-INF\classes\com\geekcer directory inside the ServletExample.

Step 2: Create a servlet, write the servlet source code.

There are three different ways to create a servlet.

  • Implementing Servlet interface
  • By extending GenericServlet class
  • By extending HttpServlet class

Create a servlet and give the name ServletProgram.java and put into the src\com\geekcer folder which was created at Step 1.

                  package com.geekcer;
                  
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletProgram implements Servlet{
  ServletConfig config = null;

  public void init(ServletConfig config) {
    this.config = config;
    System.out.println("Initialization Complete");
  }

  public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {
    res.setContentType("text/html");
    PrintWriter pwriter = res.getWriter();
    pwriter.print("<html>");
    pwriter.print("<body>");
    pwriter.print("<h1>Welcome To GeekCer!</h1>");
    pwriter.print("</body>");
    pwriter.print("</html>");
  }

  public void destroy() {
    System.out.println("Servlet life cycle finished");
  }

  public ServletConfig getServletConfig() {
    return config;
  }

  public String getServletInfo() {
    return "Copyright ©geekcer.com 2021 All Rights reserved";
  }
}

Step 3: Compile source code.

The next step is compile the above source code by following command

                  C:\apache-tomcat-8.0.30\webapps\ServletExample>javac -classpath C:\apache-tomcat-8.0.30\webapps\ServletExample\WEB-INF\lib\javax.servlet-api-3.0.1.jar C:\apache-tomcat-8.0.30\webapps\ServletExample\src\com\geekcer\ServletProgram.java
                  

Compile the source code by above command and put the created ServletProgram.class file inside the WEB-INF\classes\com\geekcer directory which was created at Step 1.

Step 4: Create a deployment descriptor (web.xml).

The web.xml file may have following configuration details.

  • Welcome Files Configuration
  • Filter Configuration
  • Servlet Configuration
  • Listener Configuration
  • Context Parameter Configuration

Create web.xml file and put the file into WEB-INF directory which was created at Step 1.

                  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>ServletTutorials</display-name>
	<servlet>
		<servlet-name>ServletProgram</servlet-name>
		<servlet-class>com.geekcer.ServletProgram</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>ServletProgram</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>
</web-app>

List of web.xml Deployment Descriptor Elements

The deployment description elements defined in the web.xml file are listed below. This has the root element <web-app>.

  • <web-app> : Root element of the web.xml file.
  • <error-code> :Defines a mapping between an error code or exception type and a Web application resource path. This element is optional.
  • <listener-class>: It refers for the name of the class that handles web application events.
  • <servlet>: It represents the servlet.
  • <servlet-name>: Represents the name of the servlet to which a URL pattern is being mapped.
  • <servlet-class>: The servlet’s fully qualified class name.
  • <servlet-mapping>: The servlet-mapping element specifies how the servlet and the URL pattern are linked.
  • <url-pattern>: The pattern for resolving URLs is described here.

Step 5: Start the server and deploy the project (After 3 and 4 Steps).

The next step you need to start the web server. For tomcat you can execute the .bat file available at Apache-Tomcat\bin\startup.bat location.

Step 6: Access the servlet (After completing all Steps).

You can call your servlet from a web browser

Paste the url on the web browser address bar
http://localhost:8088/ServletExample/welcome

What is welcome-file-list in web.xml?

The welcome file allows you to specify a list of files that the web container will use to request URLs when a URL is not mapped to a web component. This file is the first page of the web application that you allow the container to access corresponding to the application without specifying the resource name. You can also refer this page as landing page.

We define the list of welcome files by using welcome-file-list attribute of web.xml.

                  <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

If the welcome-file-list does not exist in the welcome-file-list do not exist, the server will look for the default welcome files in the following order index.html, index.htm, index.jsp, default.html, default.htm and default.jsp.

Load on startup <load-on-startup> in web.xml

If you want to create an object of a particular servlet at the time of server startup then you need to write a configuration for the servlet in the web.xml file.

                  <web-app>  
 ....  
  
  <servlet>  
    <servlet-name>first-servlet</servlet-name>  
    <servlet-class>com.geekcer.FirstServlet</servlet-class>  
    <load-on-startup>0</load-on-startup>  
  </servlet>  
  
  <servlet>  
    <servlet-name>second-servlet</servlet-name>  
    <servlet-class>com.geekcer.SecondServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  
  <servlet>  
    <servlet-name>third-servlet</servlet-name>  
    <servlet-class>com.geekcer.ThirdServlet</servlet-class>  
    <load-on-startup/>  
  </servlet>
  
 ...  
</web-app> 

The key points to remember about <load-on-startup> :

  • You can use either 0 or +ve value.
  • value >= 0 means when when the server starts the web container loads the servlet.
  • value < 0 : Whenever the container feels like it loads servlet.
  • If more than one servlet is to be loaded on the startup configuration, the container will load the servlet first, which has a lower load-on-startup value.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Servlet

Post navigation

Previous Post: Servlet Tutorial
Next Post: Servlet API | Packages and Examples of the Servlet API

More Related Articles

What is WAR file in Java What is WAR file in Java? Servlet
HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important
Servlet Tutorial Servlet Tutorial Servlet
Servlet API Servlet API | Packages and Examples of the Servlet API Servlet

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Servlet Home
  • Steps to Create a Servlet
  • Servlet API
  • What is WAR File?
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved