Talking about the web technology, Java developers will keep talking about how powerful the Spring , Struts, Wicket, JSF…..When talking about the deployment, they will say using Ant script or Maven to build or deploy. Ironically, without the IDE or technology help, many Java developers do not know either how to create a simple servlet and deploy it , nor about writing the deployment descriptor.

Nowadays , too many Java developers tight coupling with latest technology, do they know loose coupling is a good design for scalability :)? All the lasted technology is come from “Servlet” as foundation, you can not go far without it.

Here’s a quick guide to write, deploy and run a simple servlet in web container, without any IDE help.

Steps of writing your first servlet and deploy

1) Build the directory folder as following


2) Create a Java file named “”, put in the “projectname/src/com/mkyong/” folder

package com.mkyong;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDemo1 extends HttpServlet{
	public void doGe(HttpServletRequest request, HttpServletResponse response)
	throws IOException{
		PrintWriter out = response.getWriter();
		out.println("<h1>Hello Servlet Get</h1>");

3) Create a deployment descriptor named web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="" 
		<servlet-name>Servlet Name For Demo1</servlet-name>
		<servlet-name>Servlet Name For Demo1</servlet-name>

P.S servlet-name is the alias name for the ServletDemo1 class
P.S url-pattern is the url path you type in browser address bar

4) From the project directory “projectname”, compile it with following command

javac src/com/mkyong/ 
-classpath "C:\Program Files\Java\j2ee\lib\javaee.jar" -d classes

The “javaee.jar” is required for http servlet , this library is packed with J2EE SDK. In addition, all compiled classes will put in “classes” folder automatically

5) Build the directory folder in Tomcat

          \--servlet (projectname allow to change)
               \--WEB-INF (Do not change this folder name)

6) Copy all required files to Tomcat\WEB-INF folder

– “” to “Tomcat\WEN-INF\classes\com\mkyong”
– “web.xml” to “Tomcat\WEN-INF\”

                   \--web.xml [New file]
                                   \--ServletDemo1.class [New file]

7) Start Tomcat

Done ~ Launch your browser and type “http://localhost:8080/servlet/Demo1”


P.S Once the the servlet class is updated, Tomcat have to restart to take effect