Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catch events in a servlet

 
Kirke mall
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.


shankar.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic