tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jayant Kumar" <jaya...@itgindia.com>
Subject Need Help Urgently... Got Stuck!!!!
Date Mon, 06 Nov 2006 07:18:31 GMT
Hi,

This is Jayant Kr. Punjabi from iTG india Pvt. Ltd., Jaipur(INDIA). We are
using comet feature provided by Tomcat 6.0. According to  Documantation we
have implemented the CometProcessor Interface this is same as example given
in Tomcat Advanced IO documentation. but when we  sends a request(that is a
normal Http Request URL is
"http://192.168.10.28:8080/CometChatApp/ChatServlet") to that servlet always
service  method get invoked. which is contrary to Documentation that if we
implement CometProcessor always event method get invoked, if we do not
provide any service method we get the following error. - "HTTP Status 405 -
HTTP method GET is not supported by this URL - The specified HTTP method is
not allowed for the requested resource (HTTP  method GET is not supported by
this URL).".
we couldn't understand how to implement CometProcessor or send the request
to servlet(is there any other way to send request or we have to  set some
attribute in request). please help us cos we have an urgent requirment to
implemet our project on comet and we want to use the  feature provided by
tomcat container rather then using some other third party api's.

so we will be very thankful to you if u give us some guidence.

I am sending you the Servlet Code -----------------

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


public class ChatServlet  extends HttpServlet implements CometProcessor {

    protected ArrayList<HttpServletResponse> connections = 
        new ArrayList<HttpServletResponse>();
    protected MessageSender messageSender = null;
    
    public void init() throws ServletException {
    	System.out.println("Inside Init ChatServlet...");
        messageSender = new MessageSender();
        Thread messageSenderThread = 
            new Thread(messageSender, "MessageSender[" +
getServletContext().getContextPath() + "]");
        messageSenderThread.setDaemon(true);
        messageSenderThread.start();
    }
   
    public void destroy() {
    	System.out.println("Inside Destroy ChatServlet...");
        connections.clear();        
        messageSender.stop();
        messageSender = null;
    }
    
    /**
     * Process the given Comet event.
     * 
     * @param event The Comet event that will be processed
     * @throws IOException
     * @throws ServletException
     */
    public void event(CometEvent event)
        throws IOException, ServletException {
    	System.out.println("Inside event ChatServlet...");
        // Note: There should really be two servlets in this example, to
avoid
        // mixing Comet stuff with regular connection processing
        HttpServletRequest request = event.getHttpServletRequest();
        HttpServletResponse response = event.getHttpServletResponse();
        
        if (event.getEventType() == CometEvent.EventType.BEGIN) {
        	String action = request.getParameter("action");
        	System.out.println("Inside CometEvent BEGIN action =
"+action);
            if (action != null) {
                if ("login".equals(action)) {
                    String nickname = request.getParameter("nickname");
                    request.getSession(true).setAttribute("nickname",
nickname);
                    response.sendRedirect("post.jsp");
                    event.close();
                    return;
                } else {
                    String nickname = (String)
request.getSession(true).getAttribute("nickname");
                    String message = request.getParameter("message");
                    messageSender.send(nickname, message);
                    response.sendRedirect("post.jsp");
                    event.close();
                    return;
                }
            } else {
                if (request.getSession(true).getAttribute("nickname") ==
null) {
                    // Redirect to "login"
                    log("Redirect to login for session: " +
request.getSession(true).getId());
                    response.sendRedirect("login.jsp");
                    event.close();
                    return;
                }
            }
            begin(event, request, response);
        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
            error(event, request, response);
        } else if (event.getEventType() == CometEvent.EventType.END) {
            end(event, request, response);
        } else if (event.getEventType() == CometEvent.EventType.READ) {
            read(event, request, response);
        }
    }

    protected void begin(CometEvent event, HttpServletRequest request,
HttpServletResponse response)
        throws IOException, ServletException {
    	System.out.println("Inside begin ChatServlet...");

    	System.out.println("Inside Begin ");
    	request.setAttribute("org.apache.tomcat.comet", Boolean.TRUE);
        log("Begin for session: " + request.getSession(true).getId());

        PrintWriter writer = response.getWriter();
        writer.println("<!doctype html public \"-//w3c//dtd html 4.0
transitional//en\">");        
        writer.println("<head><title>JSP Chat</title></head><body
bgcolor=\"#FFFFFF\">");
        writer.flush();

        synchronized(connections) {
            connections.add(response);
        }
    }
    
    protected void end(CometEvent event, HttpServletRequest request,
HttpServletResponse response)
        throws IOException, ServletException {
    	System.out.println("Inside end ChatServlet...");
    	log("End for session: " + request.getSession(true).getId());
        synchronized(connections) {
            connections.remove(response);
        }
        request.removeAttribute("org.apache.tomcat.comet");
        PrintWriter writer = response.getWriter();
        writer.println("</body></html>");
        
        event.close();
        
    }
    
    protected void error(CometEvent event, HttpServletRequest request,
HttpServletResponse response)
        throws IOException, ServletException {
    	System.out.println("Inside error ChatServlet...");
        log("Error for session: " + request.getSession(true).getId());
        synchronized(connections) {
            connections.remove(response);
        }
        event.close();
    }
    
    protected void read(CometEvent event, HttpServletRequest request,
HttpServletResponse response)
        throws IOException, ServletException {
    	System.out.println("Inside read ChatServlet...");
        InputStream is = request.getInputStream();
        byte[] buf = new byte[512];
        do {
            int n = is.read(buf);
            if (n > 0) {
                log("Read " + n + " bytes: " + new String(buf, 0, n) 
                        + " for session: " +
request.getSession(true).getId());
            } else if (n < 0) {
                error(event, request, response);
                return;
            }
        } while (is.available() > 0);
    }



    /**
     * Poller class.
     */
    public class MessageSender implements Runnable {

        protected boolean running = true;
        protected ArrayList<String> messages = new ArrayList<String>();
        
        public MessageSender() {
        }
        
        public void stop() {
            running = false;
        }

        /**
         * Add specified socket and associated pool to the poller. The
socket will
         * be added to a temporary array, and polled first after a maximum
amount
         * of time equal to pollTime (in most cases, latency will be much
lower,
         * however).
         *
         * @param socket to add to the poller
         */
        public void send(String user, String message) {
        	System.out.println("Inside Send Message = User = "+message+"
"+user);
            synchronized (messages) {
                messages.add("[" + user + "]: " + message);
                messages.notify();
            }
        }

        /**
         * The background thread that listens for incoming TCP/IP
connections and
         * hands them off to an appropriate processor.
         */
        public void run() {

            // Loop until we receive a shutdown command
            while (running) {
                // Loop if endpoint is paused

                if (messages.size() == 0) {
                    try {
                        synchronized (messages) {
                            messages.wait();
                        }
                    } catch (InterruptedException e) {
                        // Ignore
                    }
                }

                synchronized (connections) {
                    String[] pendingMessages = null;
                    synchronized (messages) {
                        pendingMessages = messages.toArray(new String[0]);
                        messages.clear();
                    }
                    for (int i = 0; i < connections.size(); i++) {
                        try {
                            PrintWriter writer =
connections.get(i).getWriter();
                            for (int j = 0; j < pendingMessages.length; j++)
{
                                // FIXME: Add HTML filtering
                                writer.println(pendingMessages[j] + "<br>");
                            }
                            writer.flush();
                        } catch (IOException e) {
                            log("IOExeption sending message", e);
                        }
                    }
                }

            }

        }

    }




}



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


JAYANT Kr. PUNJABI
(iTG Pvt. Ltd. Jaipur, INDIA)


-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.409 / Virus Database: 268.13.24/514 - Release Date: 11/2/2006
 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message