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:

|
Current Comments
14 comments so far (post your own) View All Comments Latest 10 Comments: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
sir
i have small problem,where can i complie the servlets program i have tomcat also but i dont know how to run the program plz tell me i am a fresher i am learing java from ur site this site is enough to learn java.sequentialy tell me how to run servlets programs.
Regards,
sujit kumar
Posted by sujit kumar on Tuesday, 09.18.07 @ 03:49am | #27444
sir
i have small problem,where can i run the servlets program i have tomcat also but i dont know how to run the program plz tell me i am a fresher i am learing java from ur site this site is enough to learn java.sequentialy tell me how to run servlets programs.
Regards,
rajani
Posted by rajani on Thursday, 08.9.07 @ 16:55pm | #23021
Great web portal. Have just one question on the
text in this page. The text says "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".
Did you mean to say that goGet() method is invked by web server when it received the GET request from the client/user?? I don't quite understand what you mean by "GET request from the servlet". Please clarify
Posted by Vijay on Friday, 06.1.07 @ 00:55am | #17892
Why servlet is not having main method???
Posted by sankar prasad brahma on Friday, 05.18.07 @ 12:34pm | #16362
hi,
It's better to provide the digramatic prasntation of the folder structure from WEB-INF
Posted by Arun on Wednesday, 05.9.07 @ 19:50pm | #15512