Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Receive Server Sent Event Notifications in JAVA SERVER PAGE (JSP)

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Receive Server Sent Event Notifications in JAVA SERVER PAGE (JSP)




    Server-Sent Events - One Way Messaging
    -------------------------------------------------------------------

    A server-sent event is when a web page automatically gets updates from a server.

    This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

    Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.

    1) Create a new EventSource object, and specify the URL of the page sending the updates (in this example "ServerSendEvent" Servelet page)
    2) Each time an update is received, the onmessage event occurs
    3) When an onmessage event occurs, put the received data into the element with id="result"

    Create index.html
    ------------------------
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <h1>Getting server updates</h1>
    <div id="result"></div>

    <script>
    if(typeof(EventSource)!=="undefined")
    {
    var source=new EventSource("ServerSendEvent");
    source.onmessage=function(event)
    {
    document.getElementById("result").innerHTML+=event .data + "<br />";
    };
    }
    else
    {
    document.getElementById("result").innerHTML="Sorry , your browser does not support server-sent events...";
    }
    </script>

    </body>
    </html>

    -------------------------------------------------------------------------

    Create New servlet page in the name of 'ServerSendEvent'

    -------------------------------------------------------------------------
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * @author Senthil Kumar
    */
    public class ServerSendEvent extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("Begin for session: " + request.getSession(true).getId() + " " + response.getWriter());
    response.setHeader("pragma", "no-cache,no-store");
    response.setHeader("cache-control", "no-cache,no-store,max-age=0,max-stale=0");

    response.setContentType("text/event-stream");

    PrintWriter out = response.getWriter();

    int messagesSent = 0;
    while (true) {
    out.print("data: {" + messagesSent++ + "}\n\n");
    out.flush();
    try {
    Thread.sleep(5000);
    } catch (InterruptedException e) {
    }
    System.out.println("Sent " + messagesSent);
    }

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    }


    @Override
    public String getServletInfo() {
    return "Short description";
    }

    }
    ------------------------------------------------------------------------------





  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Receive Server Sent Event Notifications in JAVA SERVER PAGE (JSP)

    Thread moved from java theory and questions.

    Do you have a question?

Similar Threads

  1. extracting data from server parallely from jsp page
    By nischalinn in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 5th, 2012, 10:24 PM
  2. Retreiving data on page scroll down event
    By chathura in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 5th, 2012, 02:07 PM
  3. Replies: 1
    Last Post: August 12th, 2011, 10:09 AM
  4. Replies: 2
    Last Post: July 7th, 2009, 02:34 PM