深入剖析tomcat之一个简单的web服务器
2023-09-14 08:57:59 时间
在应用程序的入口点,也就是静态main函数中,创建一个HttpServer实例,然后调用其await()方法。顾名思义,await方法会在制定的端口上等待http请求,并对其进行处理,然后发送相应的消息回客户端。在接收到命令之前,它会一直保持等待的状态。
HttpServer类package simpleHttpServer; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class HttpServer { public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; private boolean shudown = false; public static void main(String[] args){ HttpServer server = new HttpServer(); server.await(); public void await(){ ServerSocket serverSocket = null; int port = 8080; try{ serverSocket = new ServerSocket(port,1,InetAddress.getByName("127.0.0.1")); catch (IOException e){ e.printStackTrace(); System.exit(1); while(!this.shudown){ Socket socket = null; InputStream input = null; OutputStream output = null; try{ socket = serverSocket.accept(); input = socket.getInputStream(); output = socket.getOutputStream(); Request request = new Request(input); request.parse(); Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); socket.close(); this.shudown = request.getUri().equals(SHUTDOWN_COMMAND); catch (Exception e){ e.printStackTrace(); continue;这个简单的web服务器,可以处理指定目录中的静态资源请求;用WEB_ROOT表示制定的目录
public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";
这里是指当前目录下的webroot文件夹下面的资源。
我们通过在游览器中输入这样的内容,进行资源的请求:
http://127.0.0.1:8080/index.html
Request类表示一个Http请求,可以传递InputStream对象来创建Request对象,调用InputStream对象的read进行Http请求数据的读取。
Request类最重要的两个函数是parse和ParseUri;parse()方法会调用私有方法parseUri来解析HTTP请求的uri,初次之外,并没有做太多的工作。parseuri会将解析的URI存储在变量uri中。
我们以 http://127.0.0.1:8080/index.html 请求为例,HTTP请求的请求行为
GET /index.html HTTP/1.1
parse()方法从传入的Request对象的InputStream对象中读取整个字节流,并且将字节数组存入缓冲区。然后用缓存区的数组初始化StringBuffer对象request。 这样再解析StringBuffer就可以解析到Uri。
*Response类
Response类表示Http相应。其定义如下
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length:23\r\n" + "\r\n" + " h1 File Not Found /h1 output.write(errorMessage.getBytes()); catch (Exception e){ e.printStackTrace(); finally{ if(fis != null){ fis.close();
使用OutputStream和Request来初始化Reponse,Response比较简单,得到Request的Uri,然后读取对应的file,如果file存在,则将file中的数据读取到缓存中,并且发送给游览器;如果file不存在,那么就发送
LNMP架构下WEB服务器如何管理及优化内存? Ngnix+PHP-FPM的工作方式,似乎是LNMP架构最节省系统资源的工作方式。当然,具体的技术选型更多的应该参考自己想要实现的业务需求。
相关文章
- Tomcat-connector的微调(1): acceptCount参数
- tomcat 启动日志乱码,idea中运行Tomcat也出现中文乱码:“淇℃伅”
- Windows平台下tomcat+java的web程序持续占cpu问题调试
- 登录tomcat服务器首页直接跳转到项目
- Web服务器之iis,apache,tomcat三者之间的比较
- 性能测试:监控web服务器--Tomcat
- 实现在tomcat中跑多个服务器实例
- 学习tomcat之通过shell批量管理多个tomcat
- CVE-2020-1938 :Apache Tomcat AJP 漏洞复现和分析
- Linux下安装Tomcat服务器和部署Web应用
- 性能测试:监控web服务器--Tomcat
- Tomcat详解(三)——tomcat多实例
- excel导出大数据量时服务器cpu过高tomcat卡死问题排查
- tomcat 服务器的几个重要监听 方法 与 使用
- JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程
- Tomcat设置最佳线程数总结
- Nagios经check_http监视web申请书server多个tomcat维修
- Tomcat服务器的默认端口是多少?怎样修改tomcat的端口?
- Tomcat卷三---Jasper引擎
- 在 MyEclipse 中配置 Tomcat
- 【项目实战】基于Tomcat服务器实现Debug模式下服务不用重启的指引设置
- Tomcat 一文带你了解tomcat体系结构
- Tomcat tomcat-users.xml帮你登录Server Status
- 安装部署tomcat服务