1

I've copied the following JSP code from another page but when I view it from a browser, the date does not display.

<HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML>

I've saved it as file.jsp.

Ganesh Sittampalam
  • 28,821
  • 4
  • 79
  • 98
sliziky
  • 129
  • 2
  • 3
  • 10
  • Welcome to Stack Overflow! I cleaned up the language of your question to make it clearer and added the 'jsp' tag to direct it to the right audience. It might also help if you provide the actual output you see in the browser, or the generated HTML accessible via "View source". – Ganesh Sittampalam Apr 06 '15 at 20:58

2 Answers2

3

Try this (file.jsp):

<%@ page import = "java.util.Date" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<html>
<head>
<%
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String date = sdf.format(new Date());
%>
</head>
<body>
<p>Hello!  The time is now <%=date%></p>
</body>
</html>

You can change the date format that suits you in "yyyy-MM-dd HH:mm:ss". And, <%=date%> displays the result. Good luck mate :)

Result:

Hello! The time is now 2015-04-07 11:25:47

Note: You have to save this file and run it from a webserver like Tomcat.

Rajesh N
  • 2,554
  • 1
  • 13
  • 17
0

You need to add the following as the first line:

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

A tutorial can be found here: http://www.java-samples.com/showtutorial.php?tutorialid=81

headlikearock
  • 665
  • 1
  • 9
  • 25
  • What webserver are you using? A JSP page is not the same as an HTML page, it must run on a webserver such as Tomcat. – headlikearock Apr 06 '15 at 20:15
  • I've noticed that by the time,now I'm trying to run it in netbeans but I cannot run Tomcat ,dont know why,"'127.0.0.1' is not recognized as an internal or external command, operable program or batch file." – sliziky Apr 06 '15 at 20:17
  • Refer this for tomcat error: http://stackoverflow.com/questions/26485487/error-starting-tomcat-from-netbeans-127-0-0-1-is-not-recognized-as-an-inter – Rajesh N Apr 07 '15 at 06:31