Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java Servlets
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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:

                         

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

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

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.