Subscribe

Java Servlet Hello World with Tomcat


Share |

Hello World Java Servlet Example , Tutorial or Sample Program

This article will is for new-bee of Java Servlet. Here I will explain you how you can run your first hello world Servlet example on Tomcat.

First download and install tomcat and JDK on your machine.

Create following folder structure inside tomcat installation directory.
[tomcat install directory]/webapps/hello
[tomcat install directory]/webapps/hello/WEB-INF
[tomcat install directory]/webapps/hello/WEB-INF/classes
[tomcat install directory]/webapps/hello/WEB-INF/classes/test

Now using any text based editor write following java code using any editor and save the file as HelloServlet.java at [tomcat install directory]/webapps/hello/WEB-INF/classes/test
package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}
After this go to command prompt and change directory to [tomcat install directory]/webapps/hello/WEB-INF/classesand give following command
javac -cp .\..\..\..\..\lib\servlet-api.jar test\HelloServlet.java

We should start understanding the servlets from the beginning. Lets start by making one program which will just print the "Hello World" on the browser. Each time the user visits this page it will display "Hello World" to the user.

As we know that the our servlet extends the HttpServlet and overrides the doGet() method which it inherits from the HttpServlet class. The server invokes doGet() method whenever web server recieves the GET request from the servlet. The doGet() method takes two arguments first is HttpServletRequest object and the second one is HttpServletResponse object and this method throws the ServletException.

Whenever the user sends the request to the server then server generates two obects, first is HttpServletRequest object and the second one is HttpServletResponse object. HttpServletRequest object represents the client's request and the HttpServletResponse represents the servlet's response.

Inside the doGet(() method our servlet has first used the setContentType() method of the response object which sets the content type of the response to text/html. It is the standard MIME content type for the Html pages. After that it has used the method getWriter() of the response object to retrieve a PrintWriter object. To display the output on the browser we use the println() method of the PrintWriter class.

Now configuring web.xml for deploying and running the Servlet. Create web.xml with following content at [tomcat install directory]/webapps/hello/WEB-INF directory
<web-app version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>test.HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Now just start tomcat server with default configuration and open following URL in your browser.

http://localhost:8080/hello/hello

You will see "Hello, world!" as an output in your browser.

Note: Before starting tomcat make sure you have set JAVA_HOME environment variable point to installation directory of JDK.


Explanation

The servlet-mapping tells Resin that the URL /hello should invoke the hello-world servlet.
The servlet tells Resin that hello-world uses the test.HelloWorld class and that the value of the greeting init parameter is Hello World.

HelloWorld.java contains the actual servlet code. It just prints a trivial HTML page filled with the greeting specified in the web.xml.

init() and destroy() are included mostly for illustration. Server will call init() when it starts the servlet and destroy before server destroys it.

Share |

No comments yet

Hot Topics

My Headlines