手写tomcat

package com.qcby.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)// 表示该注解只能用于类上
@Retention(RetentionPolicy.RUNTIME)// 表示该注解在运行时存在
public @interface ZFLServlet {String url()default "";
}

package com.qcby.config;import com.qcby.annotation.ZFLServlet;
import com.qcby.servlet.HttpServlet;
import com.qcby.util.SearchClassUtil;import java.util.HashMap;
import java.util.List;public class TomcatRoute {public static HashMap<String, HttpServlet> routes = new HashMap<>();static {List<String> paths = SearchClassUtil.searchClass();//获取全路径名//根据全路径生成对象for (String path : paths) {try {Class clazz = Class.forName(path);//生成类对象ZFLServlet webServlet = (ZFLServlet) clazz.getDeclaredAnnotation(ZFLServlet.class);System.out.println(webServlet.url());routes.put(webServlet.url(), (HttpServlet) clazz.getDeclaredConstructor().newInstance());} catch (Exception e) {e.printStackTrace();}}}}

package com.qcby.myweb;import com.qcby.annotation.ZFLServlet;
import com.qcby.requset.HttpServletRequest;
import com.qcby.response.HttpServletResponse;
import com.qcby.servlet.HttpServlet;import java.io.IOException;@ZFLServlet(url = "/delete")
public class DeleteServlet extends HttpServlet {@Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response) {super.doPost(request, response);}@Overridepublic void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {super.doGet(request, response);}
}

package com.qcby.myweb;import com.qcby.annotation.ZFLServlet;
import com.qcby.requset.HttpServletRequest;
import com.qcby.response.HttpServletResponse;
import com.qcby.servlet.HttpServlet;import java.io.IOException;
@ZFLServlet(url = "/insert")
public class InsertServlet extends HttpServlet {@Overridepublic void doGet(HttpServletRequest request, HttpServletResponse response) {System.out.println("我是insert......");}@Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response) {super.doPost(request, response);}
}

package com.qcby.servlet;import com.qcby.requset.HttpServletRequest;
import com.qcby.response.HttpServletResponse;import java.io.IOException;public abstract class HttpServlet implements servlet{public void doPost(HttpServletRequest request, HttpServletResponse response) {}public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {}@Overridepublic void service(HttpServletRequest request, HttpServletResponse response) throws IOException {if (request.getMethod().equals("GET")){doGet(request,response);} else if (request.getMethod().equals("POST")) {doPost(request,response);}}}

package com.qcby.requset;public class HttpServletRequest {private String url;private String method;public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getMethod() {return method;}public void setMethod(String method) {this.method = method;}
}
package com.qcby.response;import com.qcby.util.FileUtil;
import com.qcby.util.ResponseUtil;import java.io.File;
import java.io.IOException;
import java.io.OutputStream;public class HttpServletResponse {//输出流private OutputStream outputStream;public HttpServletResponse(OutputStream outputStream) {this.outputStream = outputStream;}/*** 返回动态资源* @param context*/public void write(String context) throws IOException {outputStream.write(context.getBytes());}/*** 返回静态资源*/public void writeHtml(String path) throws Exception {String resourcesPath = FileUtil.getResoucePath(path);File file = new File(resourcesPath);if(file.exists()){//静态文件存在System.out.println("静态文件存在");FileUtil.writeFile(file,outputStream);}else {System.out.println("静态文件不存在");write(ResponseUtil.getResponseHeader404());}}
}

package com.qcby.servlet;import com.qcby.requset.HttpServletRequest;
import com.qcby.response.HttpServletResponse;import java.io.IOException;public abstract class HttpServlet implements servlet{public void doPost(HttpServletRequest request, HttpServletResponse response) {}public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {}@Overridepublic void service(HttpServletRequest request, HttpServletResponse response) throws IOException {if (request.getMethod().equals("GET")){doGet(request,response);} else if (request.getMethod().equals("POST")) {doPost(request,response);}}}
package com.qcby.servlet;import com.qcby.requset.HttpServletRequest;
import com.qcby.response.HttpServletResponse;import java.io.IOException;public interface servlet {public void service(HttpServletRequest request, HttpServletResponse response) throws IOException;
}

package com.qcby.util;import java.io.*;public class FileUtil {public  static  boolean witeFile(InputStream inputStream, OutputStream outputStream){boolean success = false ;BufferedInputStream bufferedInputStream ;BufferedOutputStream bufferedOutputStream;try {bufferedInputStream = new BufferedInputStream(inputStream);bufferedOutputStream = new BufferedOutputStream(outputStream);bufferedOutputStream.write(ResponseUtil.responseHeader200.getBytes());int count = 0;while (count == 0){count = inputStream.available();}int fileSize = inputStream.available();long written = 0;int beteSize = 1024;byte[] bytes = new byte[beteSize];while (written < fileSize){if(written + beteSize > fileSize){beteSize = (int)(fileSize - written);bytes = new byte[beteSize];}bufferedInputStream.read(bytes);bufferedOutputStream.write(bytes);bufferedOutputStream.flush();written += beteSize;}success = true;} catch (IOException e) {e.printStackTrace();}return success;}public static boolean writeFile(File file,OutputStream outputStream) throws Exception{return witeFile(new FileInputStream(file),outputStream);}public static String getResoucePath(String path){String resource = FileUtil.class.getResource("/").getPath();return resource + "\\" + path;}
}

package com.qcby.util;/*** 设置响应头*/
public class ResponseUtil {public  static  final String responseHeader200 = "HTTP/1.1 200 \r\n"+"Content-Type:text/html\r\n"+"\r\n";public static String getResponseHeader404(){return "HTTP/1.1 404 \r\n"+"Content-Type:text/html\r\n"+"\r\n" + "404";}public static String getResponseHeader200(String context){return "HTTP/1.1 200\r\n"+"Content-Type:text/html\r\n"+"\r\n" + context;}}

package com.qcby.util;import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;public class SearchClassUtil {public static List<String> classPaths = new ArrayList<>();public static List<String> searchClass() {// 需要扫描的包名String basePack = "com.qcby.myweb";try {// 使用字符串替换Enumeration<URL> resources = SearchClassUtil.class.getClassLoader().getResources(basePack.replace(".", "/"));while (resources.hasMoreElements()) {URL url = resources.nextElement();String protocol = url.getProtocol();if ("file".equals(protocol)) {String filePath = URLDecoder.decode(url.getFile(), "UTF-8");doPath(new File(filePath), basePack);} else if ("jar".equals(protocol)) {// 处理JAR包情况System.out.println("暂未完整处理JAR包情况");}}} catch (IOException e) {e.printStackTrace();}return classPaths;}/*** 该方法会得到所有的类,将类的绝对路径写入到classPaths中** @param file 当前文件或目录* @param basePack 基础包名*/private static void doPath(File file, String basePack) {if (file.isDirectory()) {File[] files = file.listFiles();if (files != null) {for (File subFile : files) {doPath(subFile, basePack);}}} else if (file.getName().endsWith(".class")) {String classFilePath = file.getAbsolutePath();// 使用字符串替换classFilePath = classFilePath.substring(classFilePath.indexOf(basePack.replace(".", File.separator)));classFilePath = classFilePath.replace(File.separator, ".");classFilePath = classFilePath.substring(0, classFilePath.length() - 6);classPaths.add(classFilePath);}}public static void main(String[] args) {List<String> paths = searchClass();for (String path : paths) {System.out.println(path);}}
}

package com.qcby;import com.qcby.config.TomcatRoute;
import com.qcby.requset.HttpServletRequest;
import com.qcby.response.HttpServletResponse;
import com.qcby.servlet.HttpServlet;import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;public class MyTomcat {static HashMap<String, HttpServlet> routes = TomcatRoute.routes; //tomcat路由\/*** 分发器*/public  void dispatch(HttpServletRequest request ,HttpServletResponse response) throws IOException {HttpServlet servlet = routes.get(request.getUrl());  //if(servlet!=null){ //说明请求的就是我们的servletservlet.service(request,response);}}/*** socket 启动* @throws IOException*/public  void start() throws IOException {ServerSocket serverSocket = new ServerSocket(8080);  //1.指定监听的端口号//2.对端口进行监听while (true){Socket socket = serverSocket.accept();//阻塞监听//3.打开输入流,解析客户端发来的内容InputStream inputStream = socket.getInputStream(); //输入流HttpServletRequest request = new HttpServletRequest();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));  //将字节流转换成字符流String str = reader.readLine();request.setMethod(str.split("\\s")[0]);request.setUrl(str.split("\\s")[1]);//4.打开输出流OutputStream outputStream = socket.getOutputStream();HttpServletResponse response = new HttpServletResponse(outputStream);dispatch(request,response);}}//socketpublic static void main(String[] args) throws IOException {MyTomcat myTomcat = new MyTomcat();myTomcat.start();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/89633.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/89633.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android平台下openssl动态库编译

1. 下载Linux平台下的NDK软件包 NDK 下载 | Android NDK | Android Developers 下载完成后执行解压命令 # unzip android-ndk-r27d-linux.zip 2. 下载openssl-1.1.1w源码包&#xff0c;并解压 # tar -xzvf openssl-1.1.1w.tar.gz 3. 进入解压后的openssl-1.1.1w目录 …

【C++基础】面试高频考点解析:extern “C“ 的链接陷阱与真题实战

名称修饰&#xff08;Name Mangling&#xff09;是C为支持重载付出的代价&#xff0c;而extern "C"则是跨越语言边界的桥梁——但桥上的陷阱比桥本身更值得警惕 一、extern "C" 的核心概念与高频考点1.1 链接规范与名字改编机制C 为支持函数重载&#xff0…

OpenCV 官翻 4 - 相机标定与三维重建

文章目录相机标定目标基础原理代码配置校准去畸变1、使用 cv.undistort()2、使用**重映射**方法重投影误差练习姿态估计目标基础渲染立方体极线几何目标基础概念代码练习从立体图像生成深度图目标基础概念代码附加资源练习相机标定 https://docs.opencv.org/4.x/dc/dbb/tutori…

Python类中方法种类与修饰符详解:从基础到实战

文章目录Python类中方法种类与修饰符详解&#xff1a;从基础到实战一、方法类型总览二、各类方法详解1. 实例方法 (Instance Method)2. 类方法 (Class Method)3. 静态方法 (Static Method)4. 抽象方法 (Abstract Method)5. 魔术方法 (Magic Method)三、方法修饰符对比表四、综合…

VSCode使用Jupyter完整指南配置机器学习环境

接下来开始机器学习部分 第一步配置环境&#xff1a; VSCode使用Jupyter完整指南 1. 安装必要的扩展 打开VSCode&#xff0c;按 CtrlShiftX 打开扩展市场&#xff0c;搜索并安装以下扩展&#xff1a; 必装扩展&#xff1a; Python (Microsoft官方) - Python语言支持Jupyter (Mi…

数据结构与算法之美:拓扑排序

Hello大家好&#xff01;很高兴我们又见面啦&#xff01;给生活添点passion&#xff0c;开始今天的编程之路&#xff01; 我的博客&#xff1a;<但凡. 我的专栏&#xff1a;《编程之路》、《数据结构与算法之美》、《C修炼之路》、《Linux修炼&#xff1a;终端之内 洞悉真理…

Ubuntu18.04 系统重装记录

Ubuntu18.04 系统重装记录 1 安装google拼音 https://blog.csdn.net/weixin_44647619/article/details/144720947 你好&#xff01; 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章&#xff0c;了解一下Markdo…

Maven常用知识总结

Maven常用知识总结Maven 安装与配置windows mvn安装与配置IntelliJ IDEA 配置IntelliJ IDEA 配置系统mavenIntellij IDEA Maven使用IntelliJ IDEA 不能运行项目常见问题pom.xml 常用标签讲解parentgroupId artifactId versiondependencypropertiespluginpackagingdependencyMan…

PHP框架在大规模分布式系统的适用性如何?

曾几何时&#xff0c;PHP被贴上“只适合小网站”的标签。但在技术飞速发展的今天&#xff0c;PHP框架&#xff08;如Laravel、Symfony、Hyperf、Swoft等&#xff09; 早已脱胎换骨&#xff0c;勇敢地闯入了大规模分布式系统的疆域。今天&#xff0c;我们就来聊聊它的真实战斗力…

DC-DC降压转换5.5V/3A高效率低静态同步降压转换具有自适应关断功能

概述&#xff1a;PC1032是一款高效且体积小巧的同步降压转换器&#xff0c;适用于低输入电压应用。它是紧凑设计的理想解决方案。其2.5V至5.5V的输入电压范围适用于几乎所有电池供电的应用。在中等至重负载范围内&#xff0c;它以1.5MHz&#xff08;典型值&#xff09;的PWM模式…

min_25筛学习笔记+牛客多校02E

本来没有学习这种较难的算法的想法的&#xff0c;因为比赛也做不到这种难度的题&#xff0c; 但是最近打牛客多校02&#xff0c;有一题要求 [1,n][1,n][1,n] 中素数的个数&#xff0c;我以为是像莫反一样容斥&#xff0c;但是后面感觉不行。赛后知道是用 min_25 筛来求&#xf…

FunASR Paraformer-zh:高效中文端到端语音识别方案全解

项目简介 FunASR 是阿里巴巴达摩院开源的端到端语音识别工具箱,集成了多种语音识别、语音活动检测(VAD)、说话人识别等模块。其中 paraformer-zh 和 paraformer-zh-streaming 是针对中文语音识别任务优化的端到端模型,分别适用于离线和流式场景。Paraformer 采用并行 Tran…

数据结构自学Day9: 二叉树的遍历

一、二叉树的遍历“遍历”就是按某种规则 依次访问树中的每个节点&#xff0c;确保 每个节点都被访问一次且只访问一次遍历&#xff1a;前序 中序 后序&#xff08;深度优先&#xff09;&#xff0c;层序&#xff08;广度优先&#xff09;类型遍历方法特点深度优先遍历前序、中…

Leetcode(7.16)

求二叉树最小深度class Solution {public int minDepth(TreeNode root) {if (root null) {return 0;}Queue<TreeNode> queue new LinkedList<>();queue.offer(root);int depth 0;while (!queue.isEmpty()) {depth;int levelSize queue.size();for (int i 0; i…

Go从入门到精通(25) - 一个简单web项目-实现链路跟踪

Go从入门到精通(25) 一个简单web项目-实现链路跟踪 文章目录Go从入门到精通(25)前言为什么需要分布式链路跟踪&#xff1f;go实现链路跟踪搭建zipkin 服务安装依赖添加tracing包&#xff0c;OpenTelemetry 和Zipkin在 Gin 中集成 OpenTelemetry 中间件log包添加获取traceId方法…

2025年最新秋招java后端面试八股文+场景题

一、Java核心八股文&#xff08;2025年最新版&#xff09;1. Java基础HashMap vs ConcurrentHashMapHashMap&#xff1a;非线程安全&#xff0c;JDK1.8后采用数组链表/红黑树&#xff0c;扩容时可能死循环&#xff08;JDK1.7&#xff09;。ConcurrentHashMap&#xff1a;JDK1.8…

esp32 sd卡

ref&#xff1a; platform io & arduino Boards — PlatformIO latest documentation https://github.com/espressif/arduino-esp32/blob/master/libraries/SD_MMC/README.md SD 卡实验 | 极客侠GeeksMan GitHub - fabianoriccardi/ESPLogger: An Arduino library pro…

Java学习--------消息队列的重复消费、消失与顺序性的深度解析​

在 Java 分布式系统开发中&#xff0c;消息队列的应用已十分普遍。但随着业务规模扩大&#xff0c;消息的重复消费、意外消失、顺序错乱等问题逐渐成为系统稳定性的隐患。本文将从 Java 开发者的视角&#xff0c;深入分析这三大问题的产生原因、业务后果&#xff0c;并结合具体…

【Oracle】centos7离线静默安装oracle11g(p13390677_112040)

博文地址&#xff1a;https://blog.csdn.net/gitblog_06670/article/details/142569814 仓库地址&#xff1a;https://gitcode.com/Open-source-documentation-tutorial/31eb1/?utm_sourcedocument_gitcode&indexbottom&typecard 参考安装地址&#xff1a; 收费版&…

智能设备畅想

### 智能设备畅想 突然想到了一个好主意 因为最近在查无人机的相关资料&#xff08;很早之前就想搞个无人机玩玩但始终没有买&#xff09; 在了解自组装方面的内容时&#xff0c;和AI沟通了下 正好之前组装的 小智AI 基本上已经完善了&#xff0c;也正在考虑其在其他方向拓展的…