This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Page Viewed Notification ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Page Viewed Notification !" Watch "Page Viewed Notification !" New topic

Page Viewed Notification !

Udit Singh

Joined: Sep 18, 2000
Posts: 22

I am displaying a page to the user, which contains his/her personal
information. I want to update a field in the database that indicates
that the user has been presented the page. The requirement is NOT to
update that field until the info is presented to the user on the browser.
It's a MVC architecture. I could have written the updation code in the
servlet (Handler) just before it forwards the request to the final JSP
(Info page). But in this case we cannot be 100% sure that the user will be presented the page.
So we have to update the field only at the page load time. I had written a BODY onLoad script which would submit the FORM to a servlet the first time. The servlet would take care of the updation and it dispatches the same JSP. But in this case, ReLoad/Refresh effect is observed as the page is loaded ,is submitted and then again finally loaded.
We cannot write the database updation logic in the JSP.

I would really appreciate any suggestions on this.
Thanks in advance.

Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
You could use a RequestDispatcher in a servlet to include the JSP file. Then when control returns to the servlet after the JSP has written the response, you could perform your database update. This way, no database logic is in the JSP and you also know that the JSP executed without an error.
Catalin Merfu
Ranch Hand

Joined: May 26, 2004
Posts: 42
You have this problem when using any other protocols. You can not
know how the client processed the message even when you know you sent

You may explore the possibility to request the user to confirm
he viewed the page and update the flag on confirmation.

Catalin Merfu<br /><a href="" target="_blank" rel="nofollow">High Performance Java Networking</a>
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12759
I suppose you could have a JavaScript "onLoad()" function send a message when the page is actually loaded.
Udit Singh

Joined: Sep 18, 2000
Posts: 22

Thank you very much for the prompt response.
William, as I mentioned, I did try out the JavaScript "onLoad" function.
As soon as the page is loaded, onLoad submits the FORM to the Request
Handler Servlet, which after updation, returns back the same JSP. But
in this case, user observes a refreshing effect on his/her browser. That is:
First time the page loads..then suddenly disappears..and then it is
presented to the user. I could not get this solution approved

Catalin, sadly we cannot rely upon the user to give the confirmation
and this falls outside the scope of the proj requirements.

Anthony, in our architecture, the front servlet calls the request handler
servlet (which just returns the name of the JSP based on some conditions),
and the request is dispatched to the particular JSP. So basically, whatever
changes we've to make, have to be initiated at the client end.

Udit Singh

Joined: Sep 18, 2000
Posts: 22
Meanwhile, I did find a working solution, which is yet to be tested properly
and approved. The Final JSP would have 2 frames:
1st frame - 100% visible + contains the details JSP.
2nd Frame - invisible (0%) + contains a dummy JSP which would submit the request
at the load time. In this scenario, after the updation, the servlet would return
the same invisible JSP. So basically, User will just see the 1st visible frame and
he/she will not be able to notice that the invisible frame had been reloaded after

I was wondering what if we provide an image <IMG> tage at the bottom of the page,
it's src would be something like /../servlet/UpdateServlet?req_type=update_database
so once the page is loaded, the browser would search for the image and blindly fire
a request to the servlet to do the updation.
I'm not too sure how this would work if the user has disabled the image loading on the
browser !

I would really appreciate if anyone has any further suggestions on this.
Thank you very much for the replies so far. This is certainly an amazing community.

With Warm Regards,
Udit Singh
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12759
With respect to onLoad() - remember, it does not have to target the on-screen document, so you don't have to have any flash due to rebuilding the page.

Udit Singh

Joined: Sep 18, 2000
Posts: 22
Thanks for the input William. Finally, I'm implementing the onLoad which will process in the background and flash will not be observed on the screen.
I truly appreciate the help.
Warm Regards,
Udit Singh
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
A simpler (and often more robust) approach may be to use a common method employed by spammers who want to know if you have read an HTML email :roll:

Somewhere out of the way on your page, place an IMG tag which requests a 1x1 transparent gif. Serve that image dynamically from a servlet. When the image is fetched, you know that the page is being rendered. For extra credit generate a unique name for the image each time you place this "microdot" in a transmitted HTML page - then you can reliably tell which page has requested the image, and ignore page-refreshes from pages you have already logged.

This doesn't need JavaScript to be enabled in the browser, and will pretty much always work unless either the page times out during rendering, or graphics are disabled.

Use responsibly!

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Udit Singh

Joined: Sep 18, 2000
Posts: 22
Hello Frank,

Thank you. This is indeed a much simpler way of knowing whether
the page was viewed by the user or not. I was working in the same direction
earlier (IMG tag's src pointing to the servlet). But the only major issue with this implementation was : what if the user presses 'esc' to cancel the image loading or what if the image loading is disabled on the browser. Some people do cancel the full page/image loading for the faster access. :roll:
As the reliability was a major issue in the project, we could not implement the <IMG> approach.
But I still feel this was a very good idea and should be tried.
Thanks once again.

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Frank Carver:
A simpler (and often more robust) approach may be to use a common method employed by spammers who want to know if you have read an HTML email :roll:

That's interesting!
I agree. Here's the link:
subject: Page Viewed Notification !
Similar Threads
number of instances of JSP
history.back() in Netscape 7.2
JSP load time database updation..
Should we always synchronize db operations?
Should we always 'synchronize' the db operations?