Nearby lessons

3 of 34

Servlet - Your First Servlet

📌 What You Will Learn
  • Create the FirstServlet class implementing the Servlet interface
  • Configure web.xml deployment descriptor
  • Compile, deploy, and run the servlet in Tomcat
  • See complete working code examples

Your First Servlet walks you through creating, compiling, deploying, and running a complete servlet — from source code to browser response.

Step 1: FirstServlet.java

The basic purpose of a servlet is to provide a response to the end user.

Setting content type:

resp.setContentType("text/html");

MIME Type represents the type of response we are sending to the end user. The default MIME type is text/html. Other common MIME types:

  • application/pdf
  • image/jpeg
  • video/mp4

Writing to the response:

We can write character data to the response using PrintWriter:

PrintWriter out = resp.getWriter();

Text data written with this PrintWriter goes to the response and is delivered to the end user. Data written with System.out.println() goes to the server console only.

Note: Servlet programs are run by the web server, not the programmer. The main() method concept is not applicable to servlet classes — you cannot run a servlet from the command prompt.

Example01
JCode Cell
1 
2import javax.servlet.*;
3import java.io.*;
4import java.util.*;
5public class FirstServlet implements Servlet
6{
7static
8{
9System.out.println("servlet class loading...");
10}
11public FirstServlet()
12{
13System.out.println("servlet instantiation...");
14}
15public void init(ServletConfig config) throws ServletException
16{
17System.out.println("init() method execution...");
18}
19public void service(ServletRequest req,ServletResponse resp) throws ServletException,IOException
20{
21resp.setContentType("text/html");
22System.out.println("service() method execution...");
23PrintWriter out=resp.getWriter();
24out.println("<h1>Welcome Innocent Advanced Java Students</h1>");
25out.println("<h1>The Server Time is:"+new Date()+"</h1>");
26}
27public void destroy()
28{
29System.out.println("destroy() method execution...");
30}
31public ServletConfig getServletConfig()
32{
33return null;
34}
35public String getServletInfo()
36{
37return "Developed by Durga";
38}
39}
40

Step 2: Compilation

After developing the servlet class, compile it just like a normal Java class.

All dependent classes are available in servlet-api.jar, provided by the web server vendor. In Tomcat, this JAR is located at:

D:\Tomcat 7.0\lib\servlet-api.jar

Place this JAR in the classpath to compile:

classpath: D:\Tomcat 7.0\lib\servlet-api.jar

Step 3: Deployment Descriptor (web.xml)

For every web application, we provide an XML file named web.xml, also known as the Deployment Descriptor.

The web container uses this XML file to get information about our servlets — it acts as a guide to the web container.

Example03
JCode Cell
1 
2<web-app>
3<servlet>
4<servlet-name>DemoServlet</servlet-name>
5<servlet-class>FirstServlet</servlet-class>
6</servlet>
7 
8<servlet-mapping>
9<servlet-name>DemoServlet</servlet-name>
10<url-pattern>/test</url-pattern>
11</servlet-mapping>
12</web-app>
13

Steps 4-6: Folder Structure & Deployment

Step 4: Web application folder structure

Servlet API defines a standard structure that every web server can understand:

advapps1A/
  WEB-INF/
    web.xml
    classes/
      FirstServlet.class

Step 5: Deployment

Place the application folder inside the web server's deployment directory:
D:\Tomcat 7.0\webapps

Step 6: Start server and send the request

Open the browser and navigate to:

http://localhost:7777/advapps1A/test

URL PartMeaning
http://Protocol
localhostThe machine name where server is running
7777Port number on which server is running
advapps1AApplication name (Context Root)
/testurl-pattern from web.xml

Request Processing Flow

For the first request:

  1. Servlet class loading
  2. Servlet instantiation
  3. init() method called
  4. service() method called

For the second request onwards:

  1. Only service() method is called

The processing time of the first request is more compared to other requests because of class loading, instantiation, and init() execution.

<load-on-startup>

To overcome the slow first request, configure <load-on-startup> in web.xml. When configured, servlet class loading, instantiation, and init() execution happen at server startup — so even the first request only calls service().

Advantage: All requests are processed with uniform response time.

📝 Key Takeaways
  • Step-by-step servlet development process
  • Complete FirstServlet.java and web.xml code
  • Understanding load-on-startup
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3