Java实现一个简单地实现Servlet容器 | HttpServer服务

实现一个 Servlet 容器

前言

本人已经大三上了,好久没有更新了,现在第 14 周周天,学校最近课程也很少,只剩下一门J2EE开发课程了,大体内容是 Servlet + JSP 技术栈,本来不想花太多精力学习的,奈何又来一个什么软件工程专业实践、实训两门课,现在开始着手准备课设,明年上班去去参加一些比赛和认证才能得到优秀的成绩评分(属于是被绩点绑架了)...

虽然我是个前端爱好者,但是队伍里面后端也没有会的,都是基本零基础,罢了罢了,好好学一学吧(Nest 本人还不会嘤嘤嘤),起码后面学习 Spring、SpringBoot 的时候不会两眼一摸黑吧。

需要说明的是,本文只是一段代码模拟 Servlet 容器的简单实现,没有太多精深的东西,主打一个学习后的记录。如果你要问 J2EE 课程前面的内容记录呢?那我只能说摸鱼嘛懂得都懂,需要用的时候现学现卖了。

废话不多说,现在开始正文

开发环境(都不跟环境有多少关系)

环境:JDK 1.8_371、Maven 3.2.5(3.9.4 肯定也是行的)、Tomcat 8.5.93(Tomcat 9也是可以的),但是我只能说纯纯 Java 就实现了,我主要是创建了一个 Dynamic Web Application(Eclipse的说法,Intellij IDEA 应该就是 Web Application,勾选 Servlet)

顺便说说 2019 Intellij IDEA 和 2022 版本创建项目的异同,直接看下图

Intellij IDEA 2022.1.2
Intellij IDEA 2019.3.5

工具:Intellij IDEA 2019 或者 Intellij IDEA 2022

代码实现

PS: 如果要让import org.apache.commons.lang3.time.DateFormatUtils;发挥作用,需要先在pom.xml文件(Maven构建依赖)声明下载commons-lang3依赖,如下:

xml 复制代码
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
Java 复制代码
import org.apache.commons.lang3.time.DateFormatUtils;  
  
import java.io.*;  
import java.net.ServerSocket;  
import java.net.Socket;  
import java.util.Date;  
  
public class HttpServer {  
  
  public static void main(String[] args) {  
  
     try {  
        // 侦听8080端口  
        ServerSocket server = new ServerSocket(8080);  
  
        System.out.println("Info:Server start," + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));  
        while (true) {  
           // 接收客户端请求数据  
           Socket socket = server.accept();  
  
           // 读取  
           BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
           // 写入  
           BufferedWriter output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
  
           String line = input.readLine();  
  
           while (line != null && !line.isEmpty()) {  
              System.out.println(line);  
              line = input.readLine();  
           }  
           output.write("HTTP/1.1 200 OK\r\n");  
           output.write("Content-type:text/plain\r\n");  
           output.write("\r\n");  
           output.write("Success!!" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "\r\n");  
           output.flush();  
             
           //必须关闭  
           input.close();  
           output.close();  
  
           socket.close();  
        }  
     } catch (IOException e) {  
        e.printStackTrace();  
        System.out.println("Error binding the specified port.");  
     }  
  }  
}

参考资料

JAVA实现简单HTTP服务器

相关推荐
geovindu6 分钟前
python: PCA
开发语言·后端·python·算法
冰美式只要糖2 小时前
Prefill 长短请求混批,为什么 128 Token 也要等 2.58 秒?
后端
不甘平凡的小鸟2 小时前
vs code 打开spring boot项目
java·spring boot·后端
忆想不到的晖2 小时前
删掉 1800 行编排代码后,我用 Agent-Native + 薄 AOP 重写了内容工作台
后端·node.js·agent
冰美式只要糖3 小时前
Transformers 静态 KV Cache 更新更快,为什么 Decode 反而变慢?
后端
Penge6663 小时前
职场生存法则:识别服从性测试,建立自己的边界感
后端
想不到一个好的ID3 小时前
基于 Redis 的秒杀系统架构设计方案
后端
GuWenyue3 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端
Gopher_HBo4 小时前
手写 cobra 先让一个命令跑起来
后端
badhope4 小时前
Agent 说发布成功,文章却 404:我补了 4 张回读收据
后端·python·ai编程