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

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
  public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
  {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Hello World</h1>");
    out.println("</body>");
    out.println("</html>");
  }
}

web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
  <servlet>
     <servlet-name>First Servlet</servlet-name>
     <servlet-class>FirstServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>First Servlet</servlet-name>
     <url-pattern>/Servlet1</url-pattern>
  </servlet-mapping>
</web-app>

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

Related Posts

  • Abstract Class In JAVA

    Hello Friends, This tutorial is for all the Java followers. One of the best feature that is widely used is the term ‘Abstract’. This term can be used as either class or a simple method. An abstract method is any method that is just declared but not instantiated. In other words one can just create…

  • Threads in Java.

    Hello Friends, This is the tutorial for the java developers. One of the most significance feature of core java is Threading. Threading deals with the processing of Threads in a single java program. Let us learn what actually are Threads. *What are Threads? Threads are independently running processes that are isolated from each other upto…

  • Collections In Java.

    Hello friends, Welcome to another tutorial for java followers. You all may have heard about Collections, it is one of the amazing feature in java. Collections are the object for the group of elements, these elements are nothing but the different data structures like as Array Lists, Linked Lists, Vectors, Hash tables,Hash List, Trees, Hash…

  • Maven Installation.

    Hello Friends, This is one of my tutorial for installation of Maven on windows. Maven is a software project management tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. Maven is an open source community and is a…

  • Jira Plugin Deployment.

    Hello Friends, This is one of the most important tutorial for all the jira developers. You may have heard about the plugins in jira which is a simple Java Archive File with .jar extension. This JAR file contains the class files and auxiliary resources associated with the applications. In Jira we can use Eclipse IDE…

3 Comments

  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.