How can I call the get method of a servlet automatically, when a *different* HTML page is loaded?

Michael Dean

[I thought of using a image on the html page and sending the request to the servlet for the image.]

The image will work if the user enables images in his/her browser. Other solutions (JavaScript, link pre-fetching, servlet receiving requests for Cascading Style Sheets, etc.) all have similar "failure modes."

To use an image, just use a tag like:

<img src="/servlet/MySpyWareServlet" />

And return a tiny image (i.e. a 0-pixel image). (P.S. The spyware thing is a joke. :) The only way to ensure that the servlet gets called, though, is to make all clients request the servlet directly (and have the servlet serve the appropriate HTML page). This will work if you don't have external pages (i.e. not yours) pointing directly to the HTML page...

However, the Servlets 2.3 specification (i.e. the current one) has added a new feature which you could use. It allows you to define Servlet Filters--which provide a "controller-like" functionality to your web application. With a filter, you can intercept and pre-process requests and/or intercept and post-process responses to any web content on your server (i.e. Servlets, JSP's, HTML, images, CGI's, SSI's, etc.). You can even refuse requests and responses. Since it's all done on the web server, client-side configuration cannot be used to defeat this mechanism. This is the functionality you want.

[Another poster suggests using an IFRAME]