我有一个带有一个处理程序的简单netty4服务器:
public class UploadServer {
private final int port;
public UploadServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ServerInitializer());
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new UploadServer(port).run();
}
private class ServerInitializer extends ChannelInitializer<SocketChannel>{
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("decoder", new HttpRequestDecoder());
p.addLast("encoder", new HttpResponseEncoder());
p.addLast("handler", new UploadServerHandler());
}
}
和这个处理程序
public class UploadServerHandler extends SimpleChannelInboundHandler<Object> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
System.out.println("HEllO");
}
}
我有两个问题:
抱歉英语不好
答案 0 :(得分:2)
这两个&#34;你好&#34;在控制台中可能与您的浏览器正在进行两次调用有关,一次是针对index.html而另一种是针对favicon。
您可以使用curl或wget来避免请求图标。
对于url映射不同的处理程序,我这样做(不确定它是最好的方法),是我在主处理程序中获取URI:
String uri = request.getUri();
然后根据我的知识URI测试URI并相应地重定向到其他处理程序。
答案 1 :(得分:0)
Java只看一次this。让我知道这是你在寻找什么?