Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
31 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Rathin Saran
7 years ago

Very small, simple and elegant! Thanks

Joe
6 years ago

Please correct the method name from doGe to doGet in section 2.

Christian Lusardi
7 years ago

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

swati
8 years ago

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

swati
8 years ago
Reply to  swati

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

8ah9r8w3farw0
9 years ago

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

Bhawani
10 years ago

Very Easy Language to Learn . Nice way of guide.

zaheer mallik
11 years ago

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

sajad
5 years ago

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

Pratik Thacker
5 years ago

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.

Acrolink
7 years ago

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?

Sam Him
8 years ago

Please demo me using Netty, thanks 🙂

Juan Sanchez
9 years ago

step 2 – doGet

classpath tomcat – servlet-api.jar

http://localhost:8080/PROJECTNAME/Demo1

nice tutorial to start, thanks

ritesh
9 years ago

thanks.very useful

sanish
10 years ago

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

Kevin
10 years ago

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!

Miguel Ángel
10 years ago

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

Christian Lusardi
7 years ago
Reply to  Miguel Ángel

ahahaha! Yeah! Of course, it’s a misprint!

George Valergas
8 years ago
Reply to  Miguel Ángel

Such Doge. WoW

Lance E Sloan
9 years ago
Reply to  Miguel Ángel

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

Kevin Xu
10 years ago

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

Yusuf
10 years ago

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

Ozan
10 years ago
Reply to  Yusuf

You have to rename the method to ‘doGet’.

Regards
Ozan

outlet
10 years ago

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.

Cesar
11 years ago

hey…thanks for the example, served me a lot..!

amrit
11 years ago

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

Vijay Ratna
11 years ago

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

Juan Sanchez
9 years ago
Reply to  Vijay Ratna

http://localhost:8080/servlet/Demo1

change servlet to your projectname

tom
11 years ago
Reply to  Vijay Ratna

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

Rahul
11 years ago

Thanks a lot, a very basic but useful guide…

John
5 years ago

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