Home Servlets Writing Hello World
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Writing Hello World

Advertisement
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.

Writing Servlet Hello World

     

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. 

The code the program is given below:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
  public void doGet(HttpServletRequest request, 
  HttpServletResponse response
)
  throws ServletException,IOException{
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  pw.println("<html>");
  pw.println("<head><title>Hello World</title></title>");
  pw.println("<body>");
  pw.println("<h1>Hello World</h1>");
  pw.println("</body></html>");
  }
}

web.xml file for this program:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd"> -->

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

The output of the program is given below:

Download this example:

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Ask Questions?    Discuss: Writing Hello World   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
anu
March 30, 2011
please tell me where we want to save the program

hi am geeting error source not found. wer we deploye the .class file plz tell me thanq
Nagesh
April 19, 2011
Small mistake in Program

pw.println("<head><title>Hello World</title></title>"); in this line you did not closed the <head>. you have closed <title> two times.
manikandan
November 29, 2011
servlet

this is site of very usefull in all it employess...
jignesh
January 6, 2012
servlet post method

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("<html>"); pw.println("<head><title>Hello World</title></title>"); pw.println("<body>"); pw.println("<h1>Hello World</h1>"); pw.println("</body></html>"); } } how this program is done by the doPost method.........
renuka
January 11, 2012
servlet

yes this is good but i m trying to implement same... but i got error HTTP Status 404 - /demo_examples/HelloWorld type Status report message /demo_examples/HelloWorld description The requested resource (/demo_examples/HelloWorld) is not available. plz solve my problem
sreelu
January 12, 2012
servlet

i have compiled and executed servlet program but it is not getting executed.. i dont know the reason
Keith
January 16, 2012
The Code

On the first picture of code, there's a typo. The println method has 2 closing title tags
emem
April 2, 2012
article

thanks for the article :)
Anubhav
April 14, 2012
Thanks

Thank you very much
saranya
May 23, 2012
About

It is very useful to study.
MADHAN
October 21, 2013
java servlet program

please send this ipaddress in java servlet program code and notepad run method send.
MADHAN
October 21, 2013
java servlet program

how to run in java servlet program. please explain in notepad command.
DMCA.com