• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to update java.util.Calendar in jsp?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic