muhdazmilug
muhdazmilug

Reputation: 85

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 current page.

how to know a request come from web browser beside using user agent.

Upvotes: 0

Views: 1929

Answers (3)

BalusC
BalusC

Reputation: 1108632

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

}

Upvotes: 2

Boris Pavlović
Boris Pavlović

Reputation: 64622

You can use AJAX with jQuery

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240860

Invoke a javascript function on load and make a ajax call to server

Upvotes: 0

Related Questions