Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 689677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:21:25+00:00 2026-05-14T02:21:25+00:00

Hey I’m new to java servlets and I am trying to write one that

  • 0

Hey I’m new to java servlets and I am trying to write one that uses comet so that I can create a long polling Ajax request. I can successfully start the stream and perform operations but I can’t write anything out. Here is my code:

 public class CometTestServlet extends HttpServlet implements CometProcessor { 
      /**
     * 
     */
    private static final long serialVersionUID = 1070949541963627977L;
    private MessageSender messageSender = null;
    protected ArrayList<HttpServletResponse> connections = new ArrayList<HttpServletResponse>();

    public void event(CometEvent cometEvent) throws IOException, ServletException { 
        HttpServletRequest request = cometEvent.getHttpServletRequest();
        HttpServletResponse response = cometEvent.getHttpServletResponse();
        //final PrintWriter out = response.getWriter();
        if (cometEvent.getEventType() == CometEvent.EventType.BEGIN) {
            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.println("</body></html>");
            writer.flush();
            cometEvent.setTimeout(10 * 1000);
            //cometEvent.close();
        } else if (cometEvent.getEventType() == CometEvent.EventType.ERROR) { 
            log("Error for session: " + request.getSession(true).getId());
            synchronized(connections) {
                connections.remove(response);
            }
            cometEvent.close(); 
        } else if (cometEvent.getEventType() == CometEvent.EventType.END) { 
            log("End for session: " + request.getSession(true).getId());
            synchronized(connections) {
                connections.remove(response);
            }
            PrintWriter writer = response.getWriter();
            writer.println("</body></html>");
            cometEvent.close();
        } else if (cometEvent.getEventType() == CometEvent.EventType.READ) { 
            //handleReadEvent(cometEvent);
            InputStream is = request.getInputStream();
            byte[] buf = new byte[512];
            do {
                int n = is.read(buf); //can throw an IOException
                if (n > 0) {
                    log("Read " + n + " bytes: " + new String(buf, 0, n) 
                            + " for session: " + request.getSession(true).getId());
                } else if (n < 0) {
                    //error(cometEvent, request, response);
                    return;
                }
            } while (is.available() > 0);
        }
    } 

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:21:25+00:00Added an answer on May 14, 2026 at 2:21 am

    If you’re new to servlets and trying to run Comet code on Tomcat, a good start is to run the example code from http://tomcat.apache.org/tomcat-6.0-doc/aio.html.

    Very very important (from that page) is “IMPORTANT NOTE: Usage of these features requires using the APR or NIO HTTP connectors. The classic java.io HTTP connector and the AJP connectors do not support them.”

    If you don’t configure your Tomcat instance to use NIO instead of the plain HTTP connector, Comet will not work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, am trying to write to do type casting in java, but i
Hey all i have an XML file that i am trying to loop though.
Hey I'm trying to creare a link that passes the link's text directly to
Hey having some trouble trying to maintain transparency on a png when i create
Hey guys I'm trying to create a horizontal navigation on the footer of a
Hey guys I wanted to create a JScrollPane but it won't work... and I
Hey folks I am trying to implement a search functionality in a website using
Hey I'm building an app that will allow a user to log into a
Hey how can I clone the table named teamselectiontable with just the table rows
Hey I thought about writing a function that prompts me in certain situations (perl

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.