File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Displaying date in text box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying date in text box" Watch "Displaying date in text box" New topic

Displaying date in text box

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Dear friends,

I want to display the current system date in a text box present in the JSP whenever that JSP is loaded.

I tried the following
<input type="text" maxlength="9" size="" name="fromDate" value="<%out.print(new Date()); %>">

but the value is not getting displayed.
Kindly provide your help.

Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

It should be something like this.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Gobinath Manoj

Joined: Oct 17, 2007
Posts: 11

You just import util package in jsp file then you will getting the current date and time of the system. The code like ,

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>


<%//Date date=new Date();%>

date :<input type="text" value="<%= new Date()%>">


Thanks Regards <br />Gobinath.L
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Dear Freddy,

It is not working. I set the system date values in an ActionForm and tried to display it in JSP. But still it doesn't work.

How to resolve?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63547

You are not paying attention to the code examples people are showing you. out.print should rarely be used in JSP pages.
[ November 27, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

You can't use out.println() to display the value in the input text. It's a bit different from a language like php where you can just embed the PHP script inside HTML. You need to use <%= someObject.getCurrentDateTime() %>. Remember,
there's an equal sign after the percentage sign and there's no semicolon. Scriptlet is rarely used nowadays and it's considered a bad practice to use it.
I agree. Here's the link:
subject: Displaying date in text box
It's not a secret anymore!