A Simple Servlet Example – (write, deploy, run)

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

31 comments on “A Simple Servlet Example – (write, deploy, run)

  1. Your tutorial is overly complicated and complex to understand.
    Do not teach any more.

    Reply
  2. in addition :
    no 6 : “ServletDemo1.java” to “Tomcat\WEN-INF\classes\com\mkyong”
    must change to
    – “ServletDemo1.class” to “Tomcat\WEN-INF\classes\com\mkyong”

    thanks

    Reply
  3. Can you please give an example of doPut method to upload a file along with meta data in header.
    Rest service would be preferable than servlet.

    Reply
  4. second step is doGet and not doGe… can you fix this for future beginner developers?

    Reply
  5. I need to understand some very basic thing, do I need to compile and recompile every time I make some change to the source code? Is it possible to run the code interpretatively without compilation, because the compilation process takes some time. It is not possible to simply run it directly after modifications like PHP?

    Reply
  6. hi i am trying to design login page but after clicking on login button nothing is getting displayed….can u help me

    Reply
    1. i am using servlet ,html and mysql…..i want to connect db to login page and doing validation

      Reply
  7. In step 6, don’t you want to copy the class file ServletDemo1.class to WEB-INF, instead of the source file ServletDemo1.java?

    Reply
  8. if you are using TomCat you need to use the servlet-api.jar for your classpath

    Reply
  9. Just what I was looking for. I like using IDE’s but I like to do things command line first to make sure I understand the technology. Thanks for the concise write up!

    Reply
  10. I think the second point must implement the method “doGet”, not “doGe”

    Reply
    1. Also, in step 6, “WEN-INF” should be “WEB-INF”. This mistake was made twice.

      Reply
  11. Great tutorial! Your tutorial is simple and beautiful, just exactly what I am looking for to get started quickly.

    Reply
  12. HTTP Status 405 – HTTP method GET is not supported by this URL

    Reply
  13. I precisely wanted to thank you so much again. I am not sure what I would’ve tried without those tips provided by you about this problem. It was before a very distressing condition for me personally, but considering a new skilled mode you dealt with the issue made me to cry with gladness. I am just happier for the advice as well as expect you recognize what a great job you are always carrying out instructing many people with the aid of your site. I know that you haven’t met any of us.

    Reply
  14. Mr. Mkyoung, plz correct the spelling of doGet() method, i m a beginner to learn servlet, i had just copied the above code just to check a simple servlet program to run on my machine,had faced a lot to run the code,after sitting 2 days came to know the problem

    Reply
  15. hiiii
    i want to make blog…..with advertisement …….what will i do…

    Reply
  16. HTTP Status 404 – /servlet/ServletDemo1

    ——————————————————————————–

    type Status report

    message /servlet/ServletDemo1

    description The requested resource (/servlet/Demo1) is not available.

    ——————————————————————————–

    Apache Tomcat/7.0.8

    i used above given example but browser shwoing given ABOVE PLZ HELP ME

    Reply
    1. Did it compile properly? He has a typo in his example, it should read doGet and *not* doGe

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *