0

I am displaying time using Jsp and it is the home page for my application .. I am using the following code to refresh the time

<META HTTP-EQUIV="Refresh" CONTENT="1">

but it is refreshing entire page .. but my requirement is refreshing only date and not the entire page .. Help me out in solving this problem

Thanks in Advance Raj

Raj
  • 2,463
  • 10
  • 36
  • 52
  • Use javascript or jquery instead of `` refresh. – Harry Joy Sep 13 '11 at 05:01
  • I want to put this application in server machine .. so I don't to use client side scripting languages like javascript .. I don't know about jqury.. – Raj Sep 13 '11 at 05:03
  • Without using client side scripting I don't know there is any way to refresh data without refreshing the page. – Harry Joy Sep 13 '11 at 05:04
  • If you change your mind and want to use js then here is an example for this: http://www.java2s.com/Code/JavaScript/Development/Displaytimecontinueswritingtimepersecond.htm – Harry Joy Sep 13 '11 at 05:08
  • No , Thanks .. This is Server side application.. – Raj Sep 13 '11 at 05:51

1 Answers1

0

If you want partial page updates, then there is no other way than using JavaScript/Ajax (okay, you can in theory do so with a client side application like a Java applet, but that's plain clumsy). JSP runs on the server side, generates a bunch of HTML and sends it to the client side. On the client side, there is no means of any Java/JSP code. All you can do is to grab JavaScript for a bit dynamics. JavaScript is able to send HTTP requests/responses asynchronously and access/manipulate the HTML DOM tree.

Now you can work through the examples provided in How to use Servlets and Ajax? to grasp the general concepts so that you can reapply it for your own use case. Here's a basic kickoff example:

<div id="serverTime"></div>

<script>
    setTimeout(function() {
        $.get("timeServlet", function(response) {
            $("#serverTime").text(response);
        });
    }, 1000);
</script>

with the following in servlet's doGet()

response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);

response.setContentType("text/plain");
response.getWriter().write(new Date()); // Use SimpleDateFormat if necessary.
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555