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


\--projectname
    \--src
         \--com
             \--mkyong    
    \--classes
    \--WEB-INF

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


package com.mkyong;

import java.io.IOException;
import java.io.PrintWriter;
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("<html>");
		out.println("<body>");
		out.println("<h1>Hello Servlet Get</h1>");
		out.println("</body>");
		out.println("</html>");	
	}
}

3) Create a deployment descriptor named web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	
	<servlet>
		<servlet-name>Servlet Name For Demo1</servlet-name>
		<servlet-class>com.mkyong.ServletDemo1</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>Servlet Name For Demo1</servlet-name>
		<url-pattern>/Demo1</url-pattern>
	</servlet-mapping>
</web-app>

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/ServletDemo1.java 
-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


\--Tomcat
     \--webapps
          \--servlet (projectname allow to change)
               \--WEB-INF (Do not change this folder name)
                    \--classes
                         \--com
                              \--mkyong

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

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


\--Tomcat
     \--webapps
          \--servlet
               \--WEB-INF
                   \--web.xml [New file]
                    \--classes
                         \--com
                              \--mkyong
                                   \--ServletDemo1.class [New file]

7) Start Tomcat

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

servlet-example

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