分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

一个最简单的static web server

发布时间:2023-09-06 02:00责任编辑:胡小海关键词:暂无标签

  声明:资料来自Tomcat剖析一书,该程序是基于java socket的,socket和stream类作为概念背景,现在仅仅是使用其作为基础类,并不关注其本身,关注点在一个服务器的外部功能和体系结构!

  • 代码目录

  

  • HttpServer 
package simpleserver;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;/** * 说明: ??server该服务亲自管理request,和response,当前服务只是一个静态资源服务; * ????????request用来解析请求的数据流,抽象成请求对象 * ????????response根据请求路径到根目录寻找资源,并输出;该程序的响应只有响应体 * ?* 作为一个静态服务器,该程序还需要完善:响应对象本身 * @author Administrator * */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 shutdown = false; ???????public static void main(String[]args) { ???????System.out.println("打印输出web_root"+WEB_ROOT); ???????HttpServer serverSocket = new HttpServer(); ???????serverSocket.await(); ?//服务启动 ???} ???????public void await() { ???????ServerSocket serverSocket = null; ??????????int port = 8080; ???????try { ???????????serverSocket = new ServerSocket(port, 1, InetAddress.getByName("192.168.1.102"));//绑定服务的端口号,连接数量,要连接该服务的地址 ???????}catch(IOException e) { ???????????e.printStackTrace(); ???????????System.exit(1); ???????} ???????while(!shutdown) { ???????????Socket socket = null; ???????????InputStream input = null; ???????????OutputStream output = null; ???????????try { ???????????????socket = serverSocket.accept(); ??????????????//1在此监听,阻塞 ???????????????input = socket.getInputStream(); ?????????????????output = socket.getOutputStream(); ???????????????Request request = new Request(input); ??????//2把数据传递给request,令其解析;请求使用socket的输入流 ???????????????request.parse(); ????????????????Response response = new Response(output); ??//3响应对象根据已解析的请求路径,寻找资源;响应使用socket的输出流 ???????????????response.setRequest(request); ???????????????response.sendStaticResource(); ???????????????socket.close(); ???????????????????????????????//4发回响应后socket关闭;服务还在 ???????????????shutdown = request.getUri().equals(SHUTDOWN_COMMAND); ???????????} ???????????catch(Exception e) { ???????????????e.printStackTrace(); ???????????????continue; ???????????} ???????????????????} ???????????}}
View Code
  • Request
package simpleserver;import java.io.IOException;import java.io.InputStream;/** * @说明:对socket类的inputStream进行解析后生成诸多属性,隶属于该类 * @author Administrator * */public class Request { ???private InputStream input; ???private String uri; ???public Request(InputStream input) { ??????????this.input = input; ???} ???public String getUri() { ???????return uri; ???} ???public void parse() { ???????StringBuffer request = new StringBuffer(); ???????int i; ???????byte[]buffer = new ?byte[2048]; ???????try { ???????????i = input.read(buffer); ???????}catch(IOException e) { ???????????e.printStackTrace(); ???????????i = -1; ???????} ???????for(int j=0;j<i;j++) { ???????????request.append((char)buffer[j]); ???????} ???????System.out.println(request.toString()); ????????uri = parseUri(request.toString()); ???????System.out.println("截取出来的路径"+uri); ???} ???private String parseUri(String requestString) { //只解析出请求数据中的uri属性 ???????int index1, index2; ???????index1 = requestString.indexOf(‘ ‘); ???????if(index1 != -1){ ???????????index2 = requestString.indexOf(‘ ‘, index1 + 1); ???????????if(index2 > index1) { ???????????????return requestString.substring(index1+1, index2); ???????????} ???????} ???????return null; ???}}
View Code
  • Response
package simpleserver;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;public class Response { ???private static final int BUFFER_SIZE =1024; ???Request request; ???OutputStream output; ?????????public Response(OutputStream output) { ???????this.output = output; ???} ???public void setRequest(Request request) { ???????this.request = request; ???} ???????????public void sendStaticResource() throws IOException{ ???????byte[] bytes = new byte[BUFFER_SIZE]; ???????FileInputStream fis = null; ???????try { ???????????File file = new File(HttpServer.WEB_ROOT, request.getUri()); ???//1根目录拼接请求路径 ???????????System.out.println("请求路径"+file.getAbsolutePath()); ???????????if(file.exists()){ ???????????????System.out.println("文件存在"); ???????????????fis = new FileInputStream(file); ????????????????int ch = fis.read(bytes, 0, BUFFER_SIZE); ???????????????while(ch!=-1){ ???????????????????output.write(bytes, 0, ch); ????????????????????????????//2.1请求的响应数据写入输出流 ????????????????????ch = fis.read(bytes, 0, BUFFER_SIZE); ???????????????} ???????????} ???????????else { ???????????????String errorMessage = "HTTP/1.1 404 File Not Found\r\n" ????//2.2响应数据写入输出流 ???????????????????????+ "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){ ???????????System.out.println(e.toString()); ???????}finally { ???????????if(fis!=null){ ???????????????fis.close(); ???????????} ???????} ???????????} ???????}
View Code

一个最简单的static web server

原文地址:https://www.cnblogs.com/10000miles/p/9211231.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved