aspose file tools*
The moose likes Servlets and the fly likes How to use refresh in servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to use refresh in servlets?" Watch "How to use refresh in servlets?" New topic
Author

How to use refresh in servlets?

Kavinder Singh
Greenhorn

Joined: Nov 22, 2012
Posts: 4
I want my servlet to refresh in every 10 to 15 sec interval and retreive from database if any changes are made .like I am using this page and posting comment so this comment will go to database and then is is visible to all that I have posted something , in the same way if smeone replies I can see that instantly.How do we do that
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

You can use a pull approach where the browser periodically sends an XMLHttpRequest to check for and retrieve new data from the server. Push models, like COMET or HTML5 Server-Sent Events are also a possibility, but aren't uniformly supported, at least in the case of HTML5 Server-Sent Events. The idea of "refreshing a Servlet" doesn't really make sense.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16313
    
  23

A servlet is not a continuously-running process. It is a software resource that gets called when an HttpServletRequest comes into the server, produces a Response to be sent back, then returns to its caller.

Servlets also should not attempt to spawn threads to get around this limitation. It is a direct violation of the J2EE standard and can potentially corrupt the entire application server.

A Client can set itself to periodically refresh and that will initiate HttpServletRequests.


Customer surveys are for companies who didn't pay proper attention to begin with.
Kavinder Singh
Greenhorn

Joined: Nov 22, 2012
Posts: 4
if this is the case then what is the logic in discussion forums. the comments people post are going to database only. Can anyone explain the working of discussion forum
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1071
    
  13

A listener might be a possible solution to this. You could create a ContextListener that spawns a timer thread that checks periodically. Or have the system create or modify a context attribute when the database is modified and then use a ContextAttributeListener instead of a timer.

I'd have to do some experimenting to see if either of these is viable.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61788
    
  67

Jk Robbins wrote:A listener might be a possible solution to this. You could create a ContextListener that spawns a timer thread that checks periodically.

What good would that do? Without a connection to a client, there's no way to push data to a client.

Most web apps will use periodic Ajax polling to keep the client up-to-date.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1071
    
  13

Bear Bibeault wrote:What good would that do? Without a connection to a client, there's no way to push data to a client.
Most web apps will use periodic Ajax polling to keep the client up-to-date.


You're right. Now that I've had some coffee, I realize that would only set a flag when there's been a change. It still doesn't push it to a client.
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 105

Kavinder Singh wrote:if this is the case then what is the logic in discussion forums. the comments people post are going to database only. Can anyone explain the working of discussion forum


When you post comments in a discussion forum it will be submitted to the server. For example a servlet handles it, read the posted comment from the HttpRequest object and store it into a database. Later the servlet can send a redirect to the page where you post a comment. This page will then read the new content from the database, including the comment you've posted, and display it. If you don't like to see a page refresh then you need some Ajax to update the page with the latest comments from the database.


Website: Learn Java by Examples
Kavinder Singh
Greenhorn

Joined: Nov 22, 2012
Posts: 4
When you post comments in a discussion forum it will be submitted to the server. For example a servlet handles it, read the posted comment from the HttpRequest object and store it into a database. Later the servlet can send a redirect to the page where you post a comment. This page will then read the new content from the database, including the comment you've posted, and display it. If you don't like to see a page refresh then you need some Ajax to update the page with the latest comments from the database


Thank you, that mean now I have to learn AJAX as per my requirements. i will do that, thank you all for your comments.
 
wood burning stoves
 
subject: How to use refresh in servlets?