Table of Contents

"HelloWorld" Servlet with Tomcat Web server

In this tutorial, we will create a simple servlet saying “Hello World!” with Apache Tomcat v7.0.37.

Installing Apache Tomcat

  1. Download Apache Tomcat to your PC.
  2. Extract and move it to appropriate folder.
  3. Go to [your tomcat]/bin and run: startup.sh
  4. Open your browser and type: http://localhost:8080/ to verify if your tomcat is running.
  5. Back to [your tomcat]/bin and run: shutdown.sh
  6. Refresh your browser to check whether your tomcat was stopped.

Creating "HelloWorld" servlet

  1. Create a folder named “hello”: mkdir hello.
  2. Then create a folder named WEB-INF inside hello, and a folder named classes inside WEB-INF:

    cd hello
    mkdir WEB-INF
    cd WEB-INF
    mkdir classes 
  3. Create a Java class in the WEB-INF/classes folder, named it HelloServlet.java and insert the following source codes:

    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    public class HelloServlet extends HttpServlet {
     
    	protected void service(HttpServletRequest req, HttpServletResponse resp)
    	    throws ServletException, IOException {
    	    resp.setContentType("text/html");
    	    PrintWriter out = resp.getWriter();		
    	    out.println("<HTML>");
    	    out.println("<BODY>");
    	    out.println("Hello World !!!");
    	    out.println("</HTML>");
    	    out.println("</BODY>");
    	}
     
    }
  4. Compile the HelloServlet.java by executing:

    javac -cp [your tomcat]/lib/servlet-api.jar HelloServlet.java
  5. You will get a file HelloServlet.class in the same folder.
  6. In folder WEB-INF, create a file web.xml with the following content

    <web-app>
        <servlet>
                  <servlet-name>HelloServletName</servlet-name>
                  <servlet-class>HelloServlet</servlet-class>
         </servlet>
         <servlet-mapping>
                 <servlet-name>HelloServletName</servlet-name>
                 <url-pattern>/HelloWorld</url-pattern>
         </servlet-mapping>
    </web-app>

Deploying the "HelloWorld" servlet

  1. Copy the folder hello to [your tomcat]/webapps folder.
  2. Start your Tomcat. (if your Tomcat is running, your don't need to restart it because it updates applications dynamically)
  3. Open http://localhost:8080/hello/HelloWorld, you will see the message: “Hello World!!!”


Exercises

  1. Add the path “[your tomcat]/lib/servlet-api.jar” to your CLASSPATH environment variable so that when compiling a servlet project, you do not need to declare the library [your tomcat]/lib/servlet-api.jar in the command line. Step 4, part “Creating HelloWorld servlet” would become javac HelloServlet.java.
  2. Modify the HelloWorld servlet above to return a random number instead of the string “Hello World!!!”. Compile and check the result.
  3. Create another servlet that return a list of student names in HTML.
  4. Modify the servlet in exercise 3 to return the list in XML.