• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use refresh in servlets?

 
Kavinder Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kavinder Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64617
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J. Kevin Robbins
Bartender
Posts: 1725
19
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kavinder Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic