Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to Server Push when Event has occured?

Posted on 2004-10-26
2
Medium Priority
?
437 Views
Last Modified: 2008-02-01
X 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();
    }
}
0
Comment
Question by:fredand44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
kiranhk earned 150 total points
ID: 12413551
0
 

Author Comment

by:fredand44
ID: 12422445
Hello Mates!
I solved it like this, not the best way but I guess that it will work.

public class ServerReload_1g extends GeneralServlet implements HttpSessionAttributeListener
{
      static boolean send = false;

      public void start(HttpServletRequest request, HttpServletResponse response) throws IOException
      {
            ServletOutputStream servletOutputStream = response.getOutputStream();
            MultipartResponse multipartResponse = new MultipartResponse(response);

            send = true;

            int counter = 0;

            while(counter < 100)
            {
                  if(send)
                  {
                        String output = FileManager.getStringFor("applications/int_2/int_2/templates/1a.txt");
                        counter = Integer.parseInt(output);
                        multipartResponse.startResponse("text/plain");
                        servletOutputStream.println(counter);
                        multipartResponse.endResponse();
                        send = false;
                  }

                  try
                  {
                        Thread.sleep(1000);
                        counter++;
                  }
                  catch (InterruptedException e)
                  {
                  }
            }
            multipartResponse.finish();
            servletOutputStream.close();

      }

      public void attributeAdded(HttpSessionBindingEvent se)
      {
            send = true;
      }

      public void attributeRemoved(HttpSessionBindingEvent se)
      {
      }

      public void attributeReplaced(HttpSessionBindingEvent se)
      {
      }
}


public class SecureFileAccess_1a 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("applications/int_2/int_2/templates/1a.txt");
                        int counter = Integer.parseInt(output);
                        output = ""+(++counter);
                        FileManager.writeString("applications/int_2/int_2/templates/1a.txt", output, false);
                  }
                  catch(Exception e)
                  {
                        output = "1";
                        FileManager.writeString("applications/int_2/int_2/templates/1a.txt", output, false);
                  }
            }

            //Just notify the listener in ServerReload_1g that we have updated the counter
            session.setAttribute("ServerReload_1g", "ServerReload_1g");
            session.removeAttribute("ServerReload_1g");
            //It is possible to work around the listeners with using the static variable send direct.
            //ServerReload_1g.send = true;

            printString(output);
            flush();
      }
}

Thanks for your time!
Fredrik
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An overview of cyber security, cyber crime, and personal protection against hackers. Includes a brief summary of the Equifax breach and why everyone should be aware of it. Other subjects include: how cyber security has failed to advance with technol…
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question