Nearby lessons
3 of 34Servlet - Your First Servlet
- 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/pdfimage/jpegvideo/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.
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.
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 Part | Meaning |
|---|---|
| http:// | Protocol |
| localhost | The machine name where server is running |
| 7777 | Port number on which server is running |
| advapps1A | Application name (Context Root) |
| /test | url-pattern from web.xml |
Request Processing Flow
For the first request:
- Servlet class loading
- Servlet instantiation
- init() method called
- service() method called
For the second request onwards:
- 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.
- Step-by-step servlet development process
- Complete FirstServlet.java and web.xml code
- Understanding load-on-startup
- Exam-style questions at the end