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.
}
}