This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes Catch events in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Catch events in a servlet" Watch "Catch events in a servlet" New topic

Catch events in a servlet

Kirke mall

Joined: Apr 27, 2006
Posts: 1
Hi. I have to write a servlet which must be integrated in a frame of an html page.
This servlet call methods of another class that may call some events.
I want to catch these event in the servlet because the html page must automatically change depending on the state.
How can i do this?
I try to resolv my problem setting the events in a variable of the class and reading that in the servlet. So I can change the page controlling this value and refreshing the html page periodically. but, is there another method to do that without refreshing?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Hello "Kirk 'em all"-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.

Enjoy your time here.
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134

You can create your own custom Exception classes and pass on the request, response objects to it.. so that it knows what to be sent back to the servlet as an error message.

Another way would be that all other classes will just throw the exceptions and it is caught at the servlet's doGet or doPost method.


"Failure is not when you fall down; its only when you fail to get up again."
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think we need to be more precise about what happens on the server and what happens in the browser.

Most likely events are taking place on the server. If you'd like those to immediately show up on the browser you have to go outside normal HTTP protocols. This gets advanced in a hurry. My work system has an applet with an open ServerSocket so we can call out to it, but it's got problems to say the least.

A much easier approach is for the browser to occasionally ask the server for any updates. That's where you're talking about refreshing the page and you can surely avoid refreshing. You can have JavaScript on the browser call the HttpXMLService component to get data from the server every minute or whatever. Then the JavaScript updates inner HTML elements or somehow modifies the page. The user just sees the data change. They don't have to hit a key or see the whole page refresh.

If that sounds interesting we can dig into details. This architecture is called "AJAX" these days, but I'm afraid if you start Googling for AJAX you'll find a lot of confusing stuff that's way beyond your current problem.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Catch events in a servlet
It's not a secret anymore!