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
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.
- Learn the basic JavaScript concepts: JavaScript tutorial
- Learn the Ajax concepts: Ajax tutorial
- Learn the jQuery which makes Ajax/HTMLDOM very easy and crossbrowser compatible: jQuery
- Learn Servlets: our Servlets wiki page
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.
精彩评论