Reputation: 11
I'm newbie in java and jsp files. I have an application which is using java class to periodically download data from device and send it to the in jsp file:
<div id="showtime">
<%
out.print(download.show[0]); <---- this is public table in java class download
%>
</div>
Here is my question: is there any method I can use to refresh value in taken from java class without refreshing whole page or reseting session timeout countdown?
Edit: I've tried some solutions with AJAX but it doesn't work, I don't know where I made mistake:
<script>
function dateup(){
$.ajax({
type: "GET",
url: "${pageContext.request.contextPath}/update",
success: function(response){
$('#showtime').html(response);
alert("success");
},
error: function () {
alert("not success");
}
});
}
</script>
Servlet update:
@WebServlet(name = "update")
public class update extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text";
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(text);.
}
}
Mapping:
<servlet>
<servlet-name>update</servlet-name>
<servlet-class>servlets.update</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>update</servlet-name>
<url-pattern>/update</url-pattern>
</servlet-mapping>
And I changed <div>
to:
<div id="showtime"></div>
Button to trigger function:
<button onclick="dateup()">press here</button>
But it doesn't work. won't change it's context. This not trigger success function nor error one.
Upvotes: 1
Views: 394
Reputation: 11
"JSP is a server side technology, which means that if you want to refresh the page you will have to perform a request to the server which will return the new page. It is not possible to just return part of a page through normal JSP mechanisms.
If you want to just refresh a value you will need to use javascript to make an ajax call to the server to get the data you need, and repopulate the value with this data."
Upvotes: 1