简体   繁体   English

Jetty Server总是使用Handler返回404

[英]Jetty Server always returning 404 with Handler

I am working on a project and I am trying to create a server listening in the localhost address on port 8082 (I already have 8081 reserved with another server) 我正在研究一个项目,我正在尝试创建一个服务器,监听端口8082上的本地主机地址(我已经预留了8081与另一台服务器)

 public class Class2HTTPServer {
    public static void main(String[] args) {
        Class1 object1= new Class1 (300);// parameter I need for other stuff
        Class2 object2= new Class2 (300,object1,5); // parameters for other unrelated stuff
        int listen_on_port = 8082;
        Class2HTTPMessageHandler handler = new Class2HTTPMessageHandler (
            object2);
        String server_port_string = System.getProperty(SystemConstants.PROPERTY_KEY_SERVER_PORT);
        if(server_port_string != null) {
            try {
                listen_on_port = Integer.parseInt(server_port_string);
                listen_on_portfs = Integer.parseInt(server_port_string);
            } catch(NumberFormatException ex) {
                System.err.println(ex);
            }
        }
        ComponentHTTPServerUtility.createServer(listen_on_port, handler);

    }
}

This is the ComponentHTTPServerUtility class: 这是ComponentHTTPServerUtility类:

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class ComponentHTTPServerUtility {
    /**
     * Creates a server on the port and blocks the calling thread
     * @param port
     * @param handler
     * @return 
     */
    public static boolean createServer(int port, AbstractHandler handler) {
        Server server = new Server(port);
        if (handler != null) {
            server.setHandler(handler);
        }

        try {
            server.start();
            server.join();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return true;
    }

    /**
     * Creates a server on the InetAddress and blocks the calling thread
     * @param ipAddress
     * @param port
     * @param handler
     * @return 
     */
    public static boolean createServer(String ipAddress, int port,
            AbstractHandler handler) {
        InetAddress inetIpAddress;
        InetSocketAddress address;
        Server server;

        if (ipAddress == null)
            return false;

        try {
            inetIpAddress = InetAddress.getByName(ipAddress);
            address = new InetSocketAddress(inetIpAddress, port);
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            return false;
        }

        server = new Server(address);
        if (handler != null) {
            server.setHandler(handler);
        }

        try {
            server.start();
            server.join();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return true;
    }
}

When I run the Server I get the following message in the console: 当我运行服务器时,我在控制台中收到以下消息:

2015-01-19 13:13:49.451:INFO:oejs.Server:jetty-8.1.13.v20130916
2015-01-19 13:13:49.501:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8082

But If I go with my browser to : http://localhost:8082/ 但是,如果我使用我的浏览器: http:// localhost:8082 /

I get Error 404. However if I do the same with the other server(port 8081) I get a blank page indicating that the server is running. 我得到错误404.但是,如果我对其他服务器(端口8081)执行相同操作,我会得到一个空白页面,表明服务器正在运行。

Sorry but I cannot get into any more specific. 对不起,但我不能进一步具体。 Any thoughts or suggestions would be highly appreciated. 任何想法或建议将受到高度赞赏。 I have been stuck at this for hours. 我被困在这几个小时。

Thanks 谢谢

EDIT: 编辑:

These are the results i get for both servers after inserting the line: 这些是我在插入行后获得的两个服务器的结果:

System.err.println(server.dump()); 通信System.err.println(server.dump());

Working one: 工作一:

2015-01-19 15:37:05.600:INFO:oejs.Server:jetty-8.1.13.v20130916
2015-01-19 15:37:05.651:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8081
org.eclipse.jetty.server.Server@2090b38d - STARTED
 +- com.project.server.Class1HTTPMessageHandler@35f3198f - STARTED
 |
 +- qtp831407528{8<=6<=8/254,0} - STARTED
 |   +- 10 qtp831407528-10 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 11 qtp831407528-11 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 12 qtp831407528-12 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 13 qtp831407528-13 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 14 qtp831407528-14 Acceptor0 SelectChannelConnector@0.0.0.0:8081 RUNNABLE @ sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
 |   +- 15 qtp831407528-15 Selector0 RUNNABLE @ sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
 |   +- 16 qtp831407528-16 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 17 qtp831407528-17 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |
 +- SelectChannelConnector@0.0.0.0:8081 - STARTED
     +- PooledBuffers [0/1024@6144,0/1024@16384,0/1024@-]/PooledBuffers [0/1024@6144,0/1024@32768,0/1024@-] - STARTED
     +- org.eclipse.jetty.server.nio.SelectChannelConnector$ConnectorSelectorManager@42557280 - STARTED
     |   +- org.eclipse.jetty.io.nio.SelectorManager$SelectSet@77d05385 keys=0 selected=0 id=0
     |       +- org.eclipse.jetty.io.nio.SelectorManager$SelectSet.doSelect(SelectorManager.java:569)
     |       +- sun.nio.ch.WindowsSelectorImpl@4611dca9 keys=0
     +- sun.nio.ch.ServerSocketChannelImpl[/0:0:0:0:0:0:0:0:8081]
     +- qtp831407528{8<=6<=8/254,0} - STARTED

And these are from the non working one: 这些来自非工作的:

2015-01-19 15:37:23.231:INFO:oejs.Server:jetty-8.1.13.v20130916
2015-01-19 15:37:23.280:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8082
org.eclipse.jetty.server.Server@183c97db - STARTED
 +- com.project.server.Class2HTTPMessageHandler@33767f84     - STARTED
 |
 +- qtp597321988{8<=6<=8/254,0} - STARTED
 |   +- 10 qtp597321988-10 Selector0 RUNNABLE @ sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
 |   +- 11 qtp597321988-11 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 12 qtp597321988-12 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 13 qtp597321988-13 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 14 qtp597321988-14 Acceptor0 SelectChannelConnector@0.0.0.0:8082 RUNNABLE @ sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
 |   +- 15 qtp597321988-15 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 16 qtp597321988-16 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |   +- 17 qtp597321988-17 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
 |
 +- SelectChannelConnector@0.0.0.0:8082 - STARTED
     +- PooledBuffers [0/1024@6144,0/1024@16384,0/1024@-]/PooledBuffers [0/1024@6144,0/1024@32768,0/1024@-] - STARTED
     +- org.eclipse.jetty.server.nio.SelectChannelConnector$ConnectorSelectorManager@7f39425c - STARTED
     |   +- org.eclipse.jetty.io.nio.SelectorManager$SelectSet@833e745 keys=0 selected=0 id=0
     |       +- org.eclipse.jetty.io.nio.SelectorManager$SelectSet.doSelect(SelectorManager.java:569)
     |       +- sun.nio.ch.WindowsSelectorImpl@32787e9f keys=0
     +- sun.nio.ch.ServerSocketChannelImpl[/0:0:0:0:0:0:0:0:8082]
     +- qtp597321988{8<=6<=8/254,0} - STARTED

The basic handler declaration: 基本的处理程序声明:

public class Class2HTTPMessageHandler extends AbstractHandler{
     private Class2 object= null;

    public Class2HTTPMessageHandler(Class2 obj){
        object= obj;
    }
    @SuppressWarnings("unchecked")
    public void handle(String target, Request baseRequest,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        MessageTag messageTag;

        String  requestURI;
        Response fieldResponse = null;
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        requestURI = request.getRequestURI();
        if (!ComponentUtility.isEmpty(requestURI)
                && requestURI.toLowerCase().startsWith("/stock")) {
            messageTag = ComponentUtility.convertURItoMessageTag(requestURI
                .substring(6)); // the request is from store
            // manager, more
            // sophisticated security
            // features could be added
            // here
        } else {
                messageTag = ComponentUtility.convertURItoMessageTag(requestURI);
        }
        // the RequestURI before the switch
        if (messageTag == null) {
                System.out.println("Unknown message tag");
        } else {
            switch (messageTag) {
                // handling of various messages
            }
        }
    }
}

I am trying to send to the server a message tag and this is the exception I get: 我试图向服务器发送一个消息标记,这是我得到的异常:

 2015-01-19 18:16:43.739:WARN:oejs.AbstractHttpConnection:/MESSAGETAG
 java.lang.NullPointerException
     at com.project.business.Class2.functionFoo(Class2.java:98)
    at com.project.server.Class2HTTPMessageHandler.handle(Class2HTTPMessageHandler.java:69)
     at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
     at org.eclipse.jetty.server.Server.handle(Server.java:370)
     at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
     at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960)
     at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021)
     at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
     at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
     at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
     at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668)
     at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
     at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
     at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
     at java.lang.Thread.run(Thread.java:745)

When using Handlers you have to tell the server when your handler has actually handled the request. 使用处理程序时,您必须在处理程序实际处理请求时告知服务器。

Don't forget to call baseRequest.setHandled(true); 不要忘记调用baseRequest.setHandled(true);

Without this call, the handler completes and then moves onto the next handler, which in your case would be the default handler that returns 404. 如果没有这个调用,处理程序就会完成,然后移动到下一个处理程序,在你的情况下,它将是返回404的默认处理程序。

Also, when working with Handlers, use the parameters the Handler is giving you. 此外,使用Handler时,请使用Handler为您提供的参数。 The target parameter will have the path that was provided, as it was seen on the raw request. target参数将具有提供的路径,如在原始请求中看到的那样。

The request.getRequestURI() behavior relies on various ServletContext information, and as such isn't likely to be the results you are expecting when not using a ServletContext . request.getRequestURI()行为依赖于各种ServletContext信息,因此不太可能是您在不使用ServletContext时所期望的结果。

Note: if you, in fact, want a ServletContext and normal servlet behavior could be had with use of ServletContextHandler and real servlets, also within embedded mode. 注意:如果你实际上想要一个ServletContext和普通的servlet行为,可以使用ServletContextHandler和真正的servlet,也可以在嵌入模式下使用。 See ManyServletContexts example. 请参阅ManyServletContexts示例。

However, it is even easier to handle this situation. 但是,处理这种情况更容易。 Create a ContextHandlerCollection and set each Handler of yours into that collection via a ContextHandler . 创建ContextHandlerCollection并通过ContextHandler将您的每个Handler设置为该集合。 See the ManyContexts example . 请参阅ManyContexts示例

Example: 例:

ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);

ContextHandler stock = new ContextHandler("/stock");
stock.setHandler(new Class2HTTPMessageHandler());
contexts.add(stock);

With this, you can skip the entire check on the Requested URI, and let Jetty handle that. 有了这个,你可以跳过对Requested URI的整个检查,让Jetty处理它。

It should be noted that even Jetty uses the target parameter of a Handler to perform this context logic. 应该注意,即使Jetty使用Handlertarget参数来执行此上下文逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM