First Servlet Program – Hello World.

Hello Friends,

This tutorial is for the starters who want to know how exactly Servlet can be written and executed on the browser. In this, we will create a simple servlet program – Hello World.

First of all, build the Directory tree structure anywhere you want. For now I am creating it in my C Drive like –

Create a java file and save it in src folder and similarly create as web.xml file if etc folder. Classes folder is the one where .class file will be saved after compilation.

Now its time to add the code in the java file. We will discuss each elements from the below code in the later tutorials. For now you can copy and paste the below code.
FirstServlet.java file

web.xml file

Now create another folder structure inside the tomcat directory in webapps folder. The directory structure will be as:

C:\\Tomcat\\webapps\\code2java\\WEB-INF\\classes

Once the folder structure is created, put the web.xml file in the WEB-INF folder as shown above. Also,put the .class file, which we will generate from FirstServlet.java file, in the classes folder. Note that web.xml file and classes folder are both at same hierarchy in WEB-INF folder.

Now, we will compile the servlet file. The file FirstServlet. java can be compiled just like any other java file (e.g  %path%>javac filename.java), but as we will move ahead with servlets this type of compilation will not work. So, we have to use following method for compilation.

Open the command prompt (windows + R) and go to the path of the parent folder where we created java file and xml file. In this case we have the path C:code2java. Now type the following command in cmd prompt.

Root folder path>javac –classpath “path of tomcatlibservlet-api.jar ; dependencies class file path “ –d “path for class file”  path for .java file”

In the above command, javac is the java compiler command, -classpath refers to the path of servlet-api.jar file and -d is the path where the .class file should be saved after compilation (in this case the .class file will be saved in classes folder). And at last there is the path for java file which is stored in src folder. In the -classpath there is one extra value- “dependencies class file path” which is added after the jar file path divided by a “;”. This indicates the path for any class file that may be used in the java file we are compiling.

Now put the generated .class file in the directory structure C:Tomcatwebappscode2javaWEB-INF classes i.e inside the classes folder and web.xml file in the C:Tomcatwebappscode2javaWEB-INFclasses so that it will be in same hierarchy with that of classes folder.

After this, restart the tomcat server and run the link- localhost:8080/code2java/Servlet1. It Should display-Hello World

Hope this will help you.

Regards,

admin@code2java

3 thoughts on “First Servlet Program – Hello World.”

  1. Sir I create a servlet program code is
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class Hello extends HttpServlet
    {

    //@override
    public void doGet(HttpServletRequest request,HttpServletResponse response)
    {
    try{
    PrintWriter out = response.getWriter();
    out.println(“hello this is my first servlet program”);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    this code is compiled but it is not running while i have root structure just like as “ROOT>WEB-INF>web.xml file and classes folder” inside classes folder this is stored

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.