具有 comet 处理器的此 URL 不支持 HTTP 方法 GET

HTTP method GET is not supported by this URL with comet processor

提问人:kunal 提问时间:5/7/2018 最后编辑:kunal 更新时间:5/10/2018 访问量:263

问:

这可能是一个可能的重复,但没有一个答案是有帮助的。

我有一个实现 CometProcessor 的 http servlet ,我已经参考了这个例子,并将 tomcat 连接器协议切换到非阻塞 io 。但是每次我尝试运行这个项目时,它都会给我error img

错误请帮忙

下面是我的Servlet(加上帮助程序类)和web.xml

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.CometEvent;
import org.apache.catalina.CometProcessor;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;

public class TomcatWeatherServlet extends HttpServlet implements CometProcessor {

    private static final long       serialVersionUID    = 1L;
    private Logger                  logger              = Logger.getLogger(TomcatWeatherServlet.class.getName());

    private MessageSender           messageSender       = null;
    private static final Integer    TIMEOUT             = 60 * 1000;

    @Override
    public void destroy() {
        messageSender.stop();
        messageSender = null;

    }

    @Override
    public void init() throws ServletException {
        messageSender = new MessageSender();
        Thread messageSenderThread = new Thread(messageSender,
                "MessageSender[" + getServletContext().getContextPath() + "]");
        messageSenderThread.setDaemon(true);
        messageSenderThread.start();

    }

    public void event(final CometEvent event) throws IOException, ServletException {
        HttpServletRequest request = event.getHttpServletRequest();
        HttpServletResponse response = event.getHttpServletResponse();
        if (event.getEventType() == CometEvent.EventType.BEGIN) {
            request.setAttribute("org.apache.tomcat.comet.timeout", TIMEOUT);
            logger.log(Level.INFO, "Begin for session: " + request.getSession(true).getId());
            messageSender.setConnection(response);
            Weatherman weatherman = new Weatherman(95118, 32408);
            weatherman.start();
        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
            logger.log(Level.INFO, "Error for session: " + request.getSession(true).getId());
            event.close();
        } else if (event.getEventType() == CometEvent.EventType.END) {
            logger.log(Level.INFO, "End for session: " + request.getSession(true).getId());
            event.close();
        } else if (event.getEventType() == CometEvent.EventType.READ) {
            throw new UnsupportedOperationException("This servlet does not accept data");
        }

    }

    private class Weatherman {

        private final List<URL> zipCodes;
        private final String    YAHOO_WEATHER   = "http://weather.yahooapis.com/forecastrss?p=";

        public Weatherman(Integer... zips) {
            zipCodes = new ArrayList<URL>(zips.length);
            for (Integer zip : zips) {
                try {
                    zipCodes.add(new URL(YAHOO_WEATHER + zip));
                } catch (Exception e) {
                    logger.log(Level.INFO, "weather man failed");
                }
            }
        }

        public void start() {
            Runnable r = new Runnable() {

                public void run() {
                    int i = 0;
                    while (i >= 0) {
                        int j = i % zipCodes.size();
                        SyndFeedInput input = new SyndFeedInput();
                        try {
                            InputStreamReader isr = new InputStreamReader(zipCodes.get(i).openStream());
                            SyndFeed feed = input.build(isr);
                            SyndEntry entry = (SyndEntry) feed.getEntries().get(0);
                            messageSender.send(entryToHtml(entry));
                            Thread.sleep(3000L);
                        } catch (Exception e) {
                            logger.log(Level.INFO, "start failed");
                        }
                        i++;
                    }
                }
            };
            Thread t = new Thread(r);
            t.start();
        }

        private String entryToHtml(SyndEntry entry) {
            StringBuilder html = new StringBuilder("<h2>");
            html.append(entry.getTitle());
            html.append("</h2>");
            html.append(entry.getDescription().getValue());
            return html.toString();
        }
    }

    private class MessageSender implements Runnable {

        protected boolean                   running     = true;
        protected final ArrayList<String>   messages    = new ArrayList<String>();
        private ServletResponse             connection;

        private synchronized void setConnection(ServletResponse connection) {
            this.connection = connection;
            notify();
        }

        public void stop() {
            running = false;
        }

        /**
         * Add message for sending.
         */
        public void send(String message) {
            synchronized (messages) {
                messages.add(message);
                logger.log(Level.INFO, "Message added #messages=" + messages.size());
                messages.notify();
            }
        }

        public void run() {
            while (running) {
                if (messages.size() == 0) {
                    try {
                        synchronized (messages) {
                            messages.wait();
                        }
                    } catch (InterruptedException e) {
                        logger.log(Level.INFO, e.getMessage());
                    }
                }
                String[] pendingMessages = null;
                synchronized (messages) {
                    pendingMessages = messages.toArray(new String[0]);
                    messages.clear();
                }
                try {
                    if (connection == null) {
                        try {
                            synchronized (this) {
                                wait();
                            }
                        } catch (InterruptedException e) {
                            logger.log(Level.INFO, e.getMessage());
                        }
                    }
                    PrintWriter writer = connection.getWriter();
                    for (int j = 0; j < pendingMessages.length; j++) {
                        final String forecast = pendingMessages[j] + "<br>";
                        writer.println(forecast);
                        logger.log(Level.INFO, "Writing:" + forecast);
                    }
                    writer.flush();
                    writer.close();
                    connection = null;
                    logger.log(Level.INFO, "Closing connection");
                } catch (IOException e) {
                    logger.log(Level.INFO, "IOExeption sending message", e);
                }
            }
        }
    }
}



 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>WeatherServlet</servlet-name>
        <servlet-class>servlet.TomcatWeatherServlet</servlet-class> 
    </servlet>
    <servlet-mapping>
        <servlet-name>WeatherServlet</servlet-name>
        <url-pattern>/Weather</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
Java Tomcat 彗星

评论

1赞 Gatusko 5/7/2018
显示您正在使用的 Web.xml 或 Servlet。
0赞 kunal 5/8/2018
我正在使用 tomcat 9.0.0.M3

答:

0赞 Christopher Schultz 5/10/2018 #1

你有几个问题,在这里。

首先,您尚未在 servlet 中实现该方法,这就是对该资源调用 GET 返回 405 响应的原因。当您使用常规 HTTP 调用时,您期望得到什么?doGetGET /Weather

其次,Tomcat 在 8.5 中放弃了对 Comet 的支持,转而支持更标准的 WebSocket API。

因此,您根本无法将 Comet 与 Tomcat 9 一起使用。我很惊讶您甚至能够在 Tomcat 9 中初始化该 servlet。您必须从旧的 Tomcat 安装中获取一堆 JAR 文件才能使其甚至加载。但是,除非您降级到 Tomcat 8.0,否则它永远不会真正起作用。

你最好切换到 WebSocket。

评论

0赞 kunal 5/10/2018
你好。。据我对 Comet 编程的理解,上面的代码是事件驱动的,即一旦 servlet 初始化,它就会使用服务器端推送(因此不需要 doGet() IMO ) 所以预期的行为是,一旦 servlet 初始化,它将开始将数据推送到客户端,参见 WeatherMan 类。第二个是的,我后来意识到自 8.5 以来删除了对 Comet 支持的支持
0赞 Christopher Schultz 5/10/2018
Comet 确实是面向事件的,但您是在浏览器中发出普通的 HTTP 请求。您不能只在代码中键入代码并更改远程客户端与 Servlet 交互的方式。您的客户也必须知道该怎么做。implements CometProcessor