点击查看代码
package com.grady.diytomcat;

import com.grady.diytomcat.handler.DiyNettyTomcatHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List; public class DiyTomcat { private int port = 8080; public static final HashMap<String, DiyNettyServlet> SERVLET_MAPPING = new HashMap<>(); public static final HashMap<String,String> URL_MAPPING = new HashMap<>(); static {
loadServlet();
} private static void loadServlet() {
try {
//获取web.xml目录地址
String path = DiyTomcat.class.getResource("/").getPath();
SAXReader reader = new SAXReader();
//读取web.xml文件
Document document = reader.read(new File(path + "web.xml"));
//获取根标签(servlet和servlet-mapping),放在一个List中
Element rootElement = document.getRootElement();
List<Element> elements = rootElement.elements();
//循环将映射写进map映射里
for(Element element : elements){
if ("servlet".equalsIgnoreCase(element.getName())){
Element servletName = element.element("servlet-name");
Element servletClass = element.element("servlet-class");
//需要注意的是servletMapping映射的第二个参数,要通过反射的方式进行实例化
SERVLET_MAPPING.put(servletName.getText(),
(DiyNettyServlet) Class.forName(servletClass.getText().trim()).newInstance());
}else if ("servlet-mapping".equalsIgnoreCase(element.getName())){
Element servletName = element.element("servlet-name");
Element urlPattern = element.element("url-pattern");
URL_MAPPING.put(urlPattern.getText(), servletName.getText());
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
} public void start() throws IOException {
// Boss线程
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
// worker线程
NioEventLoopGroup workerGroup = new NioEventLoopGroup(); // 1.创建对象
ServerBootstrap server = new ServerBootstrap();
//2. 配置参数
server.group(bossGroup,workerGroup)
// 主线程处理类
.channel(NioServerSocketChannel.class)
// 子线程处理类 Handler
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new HttpResponseEncoder());
socketChannel.pipeline().addLast(new HttpRequestDecoder());
socketChannel.pipeline().addLast(new DiyNettyTomcatHandler());
}
})
//针对主线程的配置,最大线程数128
.option(ChannelOption.SO_BACKLOG,128)
// 针对子线程的配置,保持长连接
.childOption(ChannelOption.SO_KEEPALIVE,true); try {
// 3 启动服务器
ChannelFuture f = server.bind(port).sync();
System.out.println("DiyTomcat启动成功,监听的端口是:" + port);
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
1. netty版会稍有些不同,因为netty内部的核心是channel(对socket进行了一层封装处理),所以我们不能直接拿到socket对应的InputStreamOutputStream
2. 我们的核心处理逻辑在DiyNettyTomcatHandler
package com.grady.diytomcat.handler;

import com.grady.diytomcat.DiyNettyRequest;
import com.grady.diytomcat.DiyNettyResponse;
import com.grady.diytomcat.DiyNettyServlet;
import com.grady.diytomcat.DiyTomcat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpRequest; public class DiyNettyTomcatHandler extends ChannelInboundHandlerAdapter { @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest){
HttpRequest req = (HttpRequest) msg; DiyNettyRequest request = new DiyNettyRequest(ctx, req);
DiyNettyResponse response = new DiyNettyResponse(ctx, req); // 实际业务处理
String url = request.getUrl();
if(DiyTomcat.URL_MAPPING.containsKey(url)) {
String servletName = DiyTomcat.URL_MAPPING.get(url);
DiyNettyServlet servlet= DiyTomcat.SERVLET_MAPPING.get(servletName);
servlet.service(request, response);
} else {
response.write("404 - Not Found");
} }
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { } }

在这里,将ChannelHandlerContext 封装到DiyNettyRequest

DiyNettyResponse中,这样我们的requestresponse就拥有了读数据和写数据的能力

<br/>

源码地址:

https://github.com/ZhongJinHacker/diy-tomcat/tree/netty-tomcat

最新文章

  1. Python入门练习
  2. 推荐!手把手教你使用Git
  3. scala学习之: Flatten a nested list structure
  4. Express 路由
  5. Adapter适配器
  6. CentOS系统常用命令
  7. Linux 内核链表
  8. 防止sql注入式攻击 SQL注入学习——三层架构
  9. 如何去除 ckeditor 上传图片后在原码中留下的 style=&quot;width: 100%;height:100px&quot;之类的代码呢?
  10. 人生苦短我用Python 第三周 函数周
  11. z-index的权重是叠加的
  12. SqlServer执行Insert命令同时判断目标表中是否存在目标数据
  13. 皮皮虾FAQ
  14. java使用google开源工具实现图片压缩【转】
  15. Codeforces 362D Fools and Foolproof Roads
  16. hzy 和zsl 的生存挑战
  17. LaTeX字体设置
  18. JSON序列化反序列化
  19. c++课程学习(未完待续)
  20. python2/3 发送https请求时,告警关闭方法

热门文章

  1. SAP 文件操作类 CL_GUI_FRONTEND_SERVICES
  2. zabbix配置邮件报警
  3. 求求你们,别再刷 Star 了!这跟“爱国”没关系!
  4. 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
  5. labview入门到出家10(进阶)——CAN通讯
  6. mobaxterm会话同步
  7. NGINX屏蔽垃圾爬虫
  8. S32K148_CAN驱动(裸机开发)
  9. NOI / 2.1基本算法之枚举 1749:数字方格
  10. 2535-springsecurity系列--关于授权角色“ROLE”前缀的问题