Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Writing 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.

 

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:

                         

» View all related tutorials
Related Tags: java c database web jsp file ide browser data redirect servlet view user link page vi new this id create

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

19 comments so far (
post your own) View All Comments Latest 10 Comments:

sir
i have small problem,where can i run the servlets program i have Mysql&netbeans i just make a servlet now what i hav to doto run . i am fresher & learing java from ur site this site is enough to learn java.sequentialy tell me how to run servlets programs in html and jsp also how to run give me thats code deatils

rgds
SWATHI.S

Posted by SWATHI.S on Monday, 12.15.08 @ 00:39am | #82763

how to run servlet programes with database connection

Posted by swathi on Monday, 12.15.08 @ 00:34am | #82762

i dont how to set the environment variable path to servlet.jar.please help me
thank you...

Posted by peter on Sunday, 10.5.08 @ 00:08am | #80900

sir
i have small problem,where can i run the servlets program i have Mysql&netbeans i just make a servlet now what i hav to doto run . i am fresher & learing java from ur site this site is enough to learn java.sequentialy tell me how to run servlets programs in html and jsp also how to run give me thats code deatils

rgds
sudhakar.s


Posted by sudhakar.s on Friday, 09.19.08 @ 13:25pm | #80486

firstly i want to tell you we can create servlet by two way:--
1- By extending GenericServlet class
2- By extending HttpServlet class
in 1 we will override service method
in 2 we override processRequest method


Posted by dinesh on Friday, 08.1.08 @ 22:48pm | #70365

hai

I have wrote in one program using java servlet,but i don't know familiar to run this servlet,how can i run? can u help me?........

thanks in advance

Posted by kala on Thursday, 06.19.08 @ 11:29am | #63834

hello,
sir,
can u explain how to run servlets,
with examples

Posted by shiva on Thursday, 12.20.07 @ 17:14pm | #43114

someone said its better late than never..roseindia..u r doing an awesome job to millions of ppl around d world..thanks a lot.

Posted by dj on Wednesday, 12.5.07 @ 05:20am | #41317

sir
i have small problem,where can i run the servlets program i have tomcat & ecllips 3.1 and i know working on ecllips, i just make a servlet now what i hav to doto run . i am fresher & learing java from ur site this site is enough to learn java.sequentialy tell me how to run servlets programs on ecllips

rgds
rajesh

Posted by rajesh on Sunday, 12.2.07 @ 05:41am | #41077

is that xml language necessary to know for the learning of servelts

Posted by ramya on Tuesday, 09.18.07 @ 17:02pm | #27487

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.