File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to update java.util.Calendar in jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to update java.util.Calendar in jsp?" Watch "how to update java.util.Calendar in jsp?" New topic
Author

how to update java.util.Calendar in jsp?

Grady Jamyson
Ranch Hand

Joined: Aug 04, 2002
Posts: 42
I have an object using java.util.Calendar in jsp.
This object is to show a countdown days to a specific time.
(For example, how many days, hours, and minutes left to 2007/1/1.)

It works fine when I first get the page, but if I reload the page,
the left time doesn't change unless I restart tomcat.

I already set my application auto reload, but doing so only works
when I change my jsp code.

How do I solve this problem?
Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61759
    
  67

Not much to say without seeing your code.

Are you sure that stale pages aren't being displayed from the browser's cache?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Grady Jamyson
Ranch Hand

Joined: Aug 04, 2002
Posts: 42
My code lists as below,
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.DateFormat" %>
<%@ page contentType = "text/html;charset=utf8" %>
<%!
Calendar rightNow = Calendar.getInstance();
Calendar comeDate = Calendar.getInstance();
%>
<%
comeDate.set(2007, 0, 14,22,00);
long diff = comeDate.getTimeInMillis() - rightNow.getTimeInMillis() ;
long second = diff / 1000;
long day = second / (60 * 60 * 24);
long hour = (second - (day * 60 * 60 * 24)) / (60 * 60);
%>
...
...
<ul>
<li>Today is:<%= rightNow.get(Calendar.YEAR) + "/" + rightNow.get(Calendar.MONTH) + "/" + rightNow.get(Calendar.DAY_OF_MONTH) %>
<li>The coming date:<%= DateFormat.getInstance().format(comeDate.getTime()) %> </li>
<li>Time left: <%= day %> day <%= hour%>hour <%= min %>min</li>
</ul>

I don't think the problem is about browser's cache because I did clean the cache.
Either I restart Tomcat or I delete the compiled file can make this program work.

Thanks for reply.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61759
    
  67

Ding ding ding ding ding!



Do you understand the ramifications of using <%!?
Grady Jamyson
Ranch Hand

Joined: Aug 04, 2002
Posts: 42
It works, thanks a lot.
I don't know why the codes in that area have this kind of effect.
Would you recommend books or websites to study?

Thank you, again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61759
    
  67

Originally posted by Grady Jamyson:
It works, thanks a lot.
I don't know why the codes in that area have this kind of effect.


When you use the <%! %> construct, the declarations within are placed at the class level of the servlet that is created by the container on behalf of your JSP. Thus, your initialization was happening when the class was loaded, and never again.

This is one of the many reasons that sciptlet coding, such as you are doing, is discouraged in modern JSPs in favor of better (and less error prone) mechanisms.

Would you recommend books or websites to study?


The JSP Specification (link in JSP FAQ) is the horse's mouth,
[ December 20, 2006: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to update java.util.Calendar in jsp?