jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes servlets and sockets 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 "servlets and sockets" Watch "servlets and sockets" New topic

servlets and sockets

sbb sbb

Joined: Aug 04, 2003
Posts: 1
I need to be able to point a browser to a servlet and have the servlet continually update the browser with data. Is this possible(perhaps with the use of sockets)?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Hi Sbb,
Welcome to JavaRanch! We don't have many rules here, but we do have one: your user name ought to look like "Firstname Lastname," preferably using your real name. When you get a chance, click on My Profile and fix it, OK?
On to your question: the trick is to tell the browser to request a new page periodically.
You can include a tag like this in the <HEAD> section of the HTML your servlet sends:
<META HTTP-EQUIV=refresh CONTENT="5; URL=http://www.yourserver.com/yourservlet">

This tells the browser to load the URL http://www.yourserver.com/yourservlet in 5 seconds. If http://www.yourserver.com/yourservlet is the URL of your servlet, and the pages generated by your servlet always include this tag, then the effect is that the browser updates every 5 seconds. This won't work with every browser out there (won't work with Lynx, I don't think) but will work with most.
[ August 04, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12759
Alternate (and much more complex) approaches:
1. Use an applet for presentation of the data. The applet can repeated make requests to a servlet for new data. the advantage is that the user does not see any flashing of the page being redrawn.
2. Use JavaScript to do the same thing, loading data to an "invisible" document, then parsing out the data and updating various elements of the HTML page.
Chad Patrick

Joined: Feb 01, 2003
Posts: 11

2. Use JavaScript to do the same thing, loading data to an "invisible" document, then parsing out the data and updating various elements of the HTML page.
Any chance you would know where to find examples of this? Im doing a page where im trying to get the contents of a drop down box (or are they called drop down lists?) to dynamically change (contents of the dropdown box will be loaded by database query) , and that seems like a good approach to my problem
It is sorta covered in the JavaRanch Style Guide.
subject: servlets and sockets
Similar Threads
Tomcat 7 and HTML5 websockets support
Flying Through a page...reading the content...
How to send object from Applet to spring controller .
copying images in client-server environment?
Flying Through a page...reading the content...