java servlet : run a method in server when webpage finish onload
i want to run a method in servlet when client finished onload webpage without need to rewrite or reload the current webpage.i try to use javascript onload event, but need to rewrite back the开发者_Python百科 current page.
how to know a request come from web browser beside using user agent.
I have no idea what you mean with "but need to rewrite back the current page", but to the point, you can just use XMLHttpRequest
(Ajax) to send a HTTP request at an arbitrary moment in JavaScript.
Here's a kickoff example which utilizes jQuery so that the crossbrowsercompatible boilerplate Ajax code is minimized by a factor over 10 times, just drop it somewhere in the <head>
of your JSP/HTML document:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
$.get('servletURL', function() {
// Write here some callback function if necessary.
});
});
</script>
It will invoke the doGet()
method of a servlet mapped on /servletURL
.
@WebServlet(urlPatterns={"/servletURL"})
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Do your job here.
}
}
Invoke a javascript function on load and make a ajax call to server
You can use AJAX with jQuery
精彩评论