
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
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.