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服务器

相关推荐
舒一笑9 小时前
客户现场没有外网,Docker 服务怎么部署?
运维·后端·自动化运维
小谢小哥9 小时前
01-Java语言核心-语法特性-泛型机制详解
后端
猫咪老师9 小时前
Day4 Python的函数和参数机制
后端·python
Memory_荒年9 小时前
Netty:从“网络搬砖”到“流水线大师”的奇幻之旅
java·后端
Bear on Toilet9 小时前
接入OpenAI无法发送请求,响应为空?Bug: C++ 接入 OpenAI 中转 API
后端·ai·bug
大橙子打游戏9 小时前
Tokmon -- 监控 Claude Code 自己的 Token 消耗
后端
小码哥_常10 小时前
Spring项目新姿势:Lambda封装Service调用,告别繁琐注入!
后端
不能放弃治疗11 小时前
详解大模型对话 API,messages 角色 system 、user、assistant、tool
后端
hutengyi11 小时前
go测试问题记录
开发语言·后端·golang
青槿吖11 小时前
第二篇:Spring Boot进阶:整合异常处理、测试、多环境与日志,开发稳得一批!
java·spring boot·后端·spring·面试·sqlserver·状态模式