users@grizzly.java.net

Re: newbie help: read from console, send to browser

From: falcon <shahbazc_at_gmail.com>
Date: Wed, 2 Jul 2008 09:00:02 -0700 (PDT)

The most recent code I've tested with is almost exactly the same as Shing Wai
Chan's code listed at the end of this message. Frankly, I'm not sure
exactly where that servlet fits in.

I start the server in the following manner:

java -jar grizzly-cometd-webserver-1.8.0.jar -p 8080 -a
grizzly-cometd-echo-1.8.0.war TestServlet
(by the way, including or excluding TestServlet doesn't seem to make any
difference at all)

I open http://localhost:8080 in a couple of browsers. I type in something
in the html form, press enter and it shows up in both web pages (which is
exactly how it is supposed to work).

I don't see any exception, any of my log messages, only the startup up
message of cometd-webserver.

For example, I type in "test" in the html form, and the following gets
printed below the input field in _both_ browsers:

/service/echo: test

No other messages in the browsers or the console windows.

As I mentioned before, I'd love to figure out how to publish my own messages
to the browser.

Following is the servlet code:
-----------------
import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.sun.grizzly.comet.*;
import com.sun.grizzly.cometd.*;
import com.sun.grizzly.cometd.bayeux.*;

public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
IOException {
        doProcess(request, response);
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
IOException {
        doProcess(request, response);
    }

    protected void doProcess(HttpServletRequest request,
            HttpServletResponse response) throws ServletException,
IOException {
        String contextPath = "/cometd/cometd";
        String channel = "/service/echo";
        String messageDataName = "msg";
        String messageDataValue = request.getParameter("messageDataValue");
        ServletOutputStream out = response.getOutputStream();
        printForm(out);

        CometEngine engine = CometEngine.getEngine();
        CometContext context = engine.getCometContext(contextPath);

        if (context != null && messageDataValue != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put(messageDataName, messageDataValue);
            Data data = new Data();
            data.setMapData(map);

            DeliverResponse deliverResponse = new DeliverResponse();
            deliverResponse.setChannel("/service/echo");
            deliverResponse.setClientId("");
            deliverResponse.setData(data);
            deliverResponse.setLast(true);
            deliverResponse.setFollow(true);

            context.notify(deliverResponse);
            out.println("Data is sent.");
            System.out.println("Data is sent.");
        } else {
            out.println("No data is sent.");
            System.out.println("No data is sent.");
        }
    }

    private void printForm(ServletOutputStream out) throws IOException {
        out.println(
"<h1>Send a cometd Message</h1> <form method=\"POST\" action=\"test\">
<table> <tr><td>Message Data Value: <td><input type=\"text\"
name=\"messageDataValue\" value=\"\"> <tr><td colspan=\"2\"><input
type=\"submit\"> </table> </form> <hr>"
        );
    }
}

-- 
View this message in context: http://www.nabble.com/newbie-help%3A-read-from-console%2C-send-to-browser-tp18229898p18240950.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.