• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to update java.util.Calendar in jsp?

 
Grady Jamyson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not much to say without seeing your code.

Are you sure that stale pages aren't being displayed from the browser's cache?
 
Grady Jamyson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ding ding ding ding ding!



Do you understand the ramifications of using <%!?
 
Grady Jamyson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic