How to Server Push when Event has occured?
Hello!Does anyone got a better solution for this?
What I need to create is a servlet that uses Server side Push/Server sid Reload. When something happens a page should be reloaded.
Suppose that you got a visit counter at a page in a browser and if that counter is updated from an other page in an other browser all other page in all browsers with that counter needs to be updated.
I have created a ugly hack that produce this, but I'm not satisfied with it. My hack does a file access every second and I do not want that.
What I would like is some Listener that listens if perhaps the file is changed or some other Event has occur on the server side but I do not know if that is possible.
Below is my ugly hack divided in 2 Servlets. My FileManager just reads a number from a file, nothing magic.
So if any one got a nice solution please let me know!
BTW Server side Push doesn't work in Internet Explorer I use a old NetScape at Linux Mandrake 8.0 that works fine. I have heard that it is possible with FireFox as well.
Best regards
Fredrik
The servlets that checks for changes:
package my.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import my.servlets.*;
import my.tools.*;
import com.oreilly.servlet.*;
import com.oreilly.servlet.multipart.*;
public class ServerReload extends GeneralServlet
{
public void start(HttpServletRequest request, HttpServletResponse response) throws IOException
{
ServletOutputStream out = response.getOutputStream();
MultipartResponse multi = new MultipartResponse(response);
int counter = 0;
while(counter < 100)
{
String output = FileManager.getStringFor("counter.txt");
int tempCounter = Integer.parseInt(output);
if(counter != tempCounter)
{
multi.startResponse("text/plain");
counter = tempCounter;
out.println(counter);
multi.endResponse();
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
}
multi.finish();
out.close();
}
}
The servlet that updates the counter.txt file:
package my.servlets.assignments1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import my.servlets.*;
import my.tools.*;
public class SecureFileAccess extends GeneralServlet
{
public void start(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
out = new PrintWriter(response.getOutputStream());
String output = null;
synchronized(this)
{
try
{
output = FileManager.getStringFor("counter.txt");
int counter = Integer.parseInt(output);
output = ""+(++counter);
FileManager.writeString("counter.txt", output, false);
}
catch(Exception e)
{
output = "1";
FileManager.writeString("counter.txt", output, false);
}
}
printString(output);
flush();
}
}
