gpt4 book ai didi

java - Jetty 服务器总是使用处理程序返回 404

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:38 25 4
gpt4 key购买 nike

我正在做一个项目,我正在尝试创建一个服务器来监听端口 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);

}
}

这是 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;
}
}

当我运行服务器时,我在控制台中收到以下消息:

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

但是如果我用我的浏览器去: http://localhost:8082/

我收到错误 404。但是,如果我对另一台服务器(端口 8081)执行相同操作,我会收到一个空白页面,表明该服务器正在运行。

抱歉,我不能说得更具体。任何想法或建议将不胜感激。我已经坚持了几个小时。

谢谢

编辑:

这些是我在插入该行后从两台服务器获得的结果:

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

工作一个:

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

这些来自非工作的:

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

基本处理程序声明:

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
}
}
}
}

我正在尝试向服务器发送消息标签,这是我得到的异常:

 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)

最佳答案

使用处理程序时,您必须在处理程序实际处理请求时告诉服务器。

别忘了调用baseRequest.setHandled(true);

如果没有此调用,处理程序将完成,然后移至下一个处理程序,在您的情况下,它将是返回 404 的默认处理程序。

此外,在使用处理程序时,请使用处理程序提供给您的参数。 target 参数将具有所提供的路径,就像在原始请求中看到的那样。

request.getRequestURI() 行为依赖于各种 ServletContext 信息,因此在不使用 时不太可能是您期望的结果>Servlet上下文

注意:如果您实际上想要一个 ServletContext 并且正常的 servlet 行为可以通过使用 ServletContextHandler 和真正的 servlet 实现,也在嵌入式模式下. See ManyServletContexts example.

但是,处理这种情况更容易。创建一个 ContextHandlerCollection 并通过 ContextHandler 将您的每个 Handler 设置到该集合中。查看ManyContexts example .

示例:

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

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

有了这个,您可以跳过对 Requested URI 的整个检查,让 Jetty 处理。

应该注意的是,即使 Jetty 也使用 Handlertarget 参数来执行此上下文逻辑。

关于java - Jetty 服务器总是使用处理程序返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28024663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com