Wednesday, April 13, 2011

Displaying time in JSP page.

This was my line in demo.jsp demo

Hello there!!! The time is <%= new java.util.Date()%>

which when i opened in firefox doesn't show the time. instead displays the same line: "The time is <%= new java.util.Date()%>"

From stackoverflow
  • If you go to this link, it has examples, with source, that show you how to do it properly. See the "Date" example under JSP 1.2 examples.

    Another recommendation: Learn JSTL and use its format tags to format time and date properly. Don't use scriptlets.

  • It looks like you're putting <%= new java.util.Date()%> in the wrong place, and it is being treated as text rather than code, it should look something like this:

    <td width="100%"><b>&nbsp;Current  Date 
    and time is:&nbsp; <font color="#FF0000">
    
    
    <%= new java.util.Date() %>
    </font></b></td>
    

    If you post a code sample, it'll help a lot.

    Some examples here too: http://www.roseindia.net/jsp/jsp_date_example.shtml

    Dave

  • I'm assuming that your JSPs are compiling.

    The <%= %> notation implicitly invokes the instance's toString() method. Since constructors in Java don't return a reference to the newly created instance, you're trying to do this:

    new java.util.Date().toString() //! illegal
    

    Try this instead:

    <% java.util.Date now = new java.util.Date() %>
    <%= now %>
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.