Steps to Write Java Servlet Program

Servlets » on Jan 5, 2013 { 6 Comments } By Sivateja

Let us see the basic steps to develop java servlet application…

Servlet program is not like, writing java code and execute through command prompt.  We need to follow the following steps in order to develop any servlets program.  Even for a simple HelloWorld program also one must follow this standard directory structure which is prescribed by sun.

  • Create one root directory with your own name, and create another directory within that directory with name ‘src’ and now write one servlet program and copy into that ‘src’ folder
  • Now create another folder ‘web-inf’ in the root directory, this web-inf folder contains web.xml file will see later
  • Create ‘classes’ folder with in web-inf folder
  • Compile our servlet program which is in src folder, so we will get one .class file right, just copy this .class file into classes folder in web-inf folder
  • By the way, we are going to run our servlet by using Tomcat server.  So we must set the class path for Tomcat related jar file, servlet-api.jar
  • You can find this jar file in your Tomcat directory\common\lib\,
  • Similarly if we are using Weblogic server, we need to set the class path for weblogic related jar files, i.e weblogic.jar which is in c [your drive]:\bea\weblogic\server\lib
  • If our servlet is dealing with any database like MySql or Oracle, it is mandatory for us to create a folder  ‘lib’ with in web-inf folder
  • Once everything is done, just copy our root director into Tomcat/webapps folder, and start the server and check the output, i will show you how in the first application

Directory Structure

Steps To Develop Servlet Program

  • Set servlet-api.jar ( Will be available in your tomcat/lib folder ) in your class path
  • Open notepad or any text editor EditPlus or some thing
  • Import javax.servlet.*;
  • Import javax.servlet.http.*;
  • Create one user defined class whose modifier must be public, which must extends javax.servlet.GenericServlet[Not recommended] or  javax.servlet.http.HttpServlet[recommended]
  • We know GenericServlet implementing Servlet interface,  and HttpServlet is the sub class of GenericServlet.  So by default all life cycle methods will be overridden in GenericServlet class, so just override the required [No need all] life cycle methods of servlet in HttpServlet.
  • Save your servlet program by giving an extension .java
  • You must place your .java files in root folder or in src folder, and .class files in classes files
  • Compile and check the result

Yeah these are the basic and default steps for writing any servlet application, i will tell you the more(web.xml, deployments and all…) while we are working with the examples 😉


Once everything is done, just copy our root director into Tomcat/webapps folder, and start the server and check the output, i will show you in the first example.


You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


6 Responses to “Steps to Write Java Servlet Program”
  1. Dipti says:

    How to make the servlet thread safe?

  2. Dear Admin,
    Here in “steps to develop servlet program”, i think it should be “javax.servlet.GenericServlet” rather than “java.servlet.GenericServlet” in the 5th point. And “classes folder” instead of “classes file” in 8th point.

    Your tutorial is really very good. It helps me a lot in understanding the basics. 🙂

  3. Java4s says:

    @Ashit Ranjan Samal

    Thanks you, it’s updated now 🙂

  4. nabil says:

    how can we update servlet?

  5. naresh says:

    Hi sir,
    whe i compiled a servlet in command prompt, then it shows an error that is ..”javac: file not found:
    Usage: javac ”

    Please give solution for this

  6. Karthickraja.M says:

    Thanks for your Great work on Java Servlet
    I am the beginner
    Your article and directory image


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.