Reputation: 21
so I'm quite new to JSP, servlets and the like although I'm quite comfortable with all of the concepts in an MVC framework. In the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="js/jquery.js"></script>
<title>Some title</title>
</head>
<body>
<div id="mostRecent">
Most Recent Time: <span id="mTime"></span><br>
</div>
<input id="butt" type="button" value="refresh" />
<script type="text/javascript">
$(document).ready(function(){
count = 0;
$('#butt').click(function(){
$('#mTime').text(" <%= new java.util.Date() %> ");
count += 1;
});
});
</script>
</body>
</html>
When I click the refresh button the current date is written in the span as expected, but only the first time it is called ie. New more current date strings are not added, making me think that the method is being called (successfully) only once. Is there any way around this? Or even better, is there a problem in my general technique (ie. should I be making a request to a servlet for the time as to not add too much view logic). If that is the case then a description as to how I could load a servlet response into only a small part of the page (rather than printing the response to a new page) would be appreciated. Many thanks.
Upvotes: 0
Views: 1210
Reputation: 968
Well your example don't work because the page is interpreted only once, i.e. the jsp page is interpreted when the it is requested. To solve your problem you can create a JSP page with the code:
<%= new java.util.Date() %>
You can try something like this:
$('#butt').click(function(){
$.get("get_current_date.jsp", function(response) {
$('#mTime').text(response);
count += 1;
});
});
Upvotes: 1