Displaying Date in Servlet

In this example we are going to show how we can display
a current date and time on our browser. It is very easy to display it on our
browser by using the Date class of the java.util package.
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. The MIME type tells the browser what kind of data the
browser is about to receive. 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 java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DisplayingDate extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
PrintWriter pw = response.getWriter();
Date today = new Date();
pw.println("<html>"+"<body><h1>Today Date is</h1>");
pw.println("<b>"+ today+"</b></body>"+ "</html>");
}
}
|
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>DateDisplay</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/DateDisplay</url-pattern>
</servlet-mapping>
</web-app>
|
The output of the program is given below:
Download this example:

|
Current Comments
10 comments so far (post your own) View All Comments Latest 10 Comments:Its very good for those who are willing to learn the Java Technology. All the Concepts and the examples are very easy to learn and Understand. Now, I am learning the advance Java concepts through our site.
thanks for roseindia.
Posted by Shanthusharma on Thursday, 09.18.08 @ 09:55am | #80247
Now i am a software Trainee....Your website is very easy to learn for the beginners...It contains each and every steps that we follow ......It is very nice....
Posted by Nanthakumar on Friday, 09.7.07 @ 20:19pm | #26324
i want the validation of time and time and date
2)string tokinezer
Posted by inti. pradeep on Saturday, 07.14.07 @ 11:24am | #21266
This site is very good for freshers to know about java ..
Posted by pavan kumar chelluri on Thursday, 07.5.07 @ 11:55am | #20749
Now, you can get the correct example with the correct code and description.
Thanks for pointing out the error.
Chandan Kumar Verma
Roseindia Member
Posted by Chandan Kumar Verma on Thursday, 04.19.07 @ 17:05pm | #14566
This site is very good.
But take care of the programs and their output coming on the screen. In the above program to print date no code is written. So please rectify it as early as you can.
Thank you.
Posted by Padma on Wednesday, 04.18.07 @ 16:26pm | #14498
I think u have put the wrong program.It is same as previous "Hello World" program.
Posted by huzefa on Tuesday, 04.17.07 @ 09:31am | #14400
the o/p of the program is incorrect...the object pw prints "hello world" and not the date...there are no methods which invoke the date from the system
Posted by anirbanorty on Monday, 04.16.07 @ 13:28pm | #14314
today i am reading this page.in the above program your output does not match with your coding part,so as soon as possible rectify it.
thanks
Posted by lippon kumar choudhury on Thursday, 03.22.07 @ 10:46am | #12466
This site is very nice and good for biginner of java.
The program of servlet for displaying date is not correct because it shows HelloWorld output
Posted by Shivkumar Tadas on Thursday, 03.15.07 @ 11:05am | #11767