how tomcat works 读书笔记(一)----------一个简单的web服务器
http协议
若是两个人能正常的说话交流,那么他们间必定有一套统一的语言规则<在网络上服务器与客户端能交流也依赖与一套规则,它就是我们说的http规则(超文本传输协议Hypertext transfer protocol)。
http分两部分,一个是请求(客户端发给服务器),一个是回复(服务器发给客户端)。
先看http请求
下面就是一个http请求的例子,其中的各项参数,请查阅相关资料。(http://www.cnblogs.com/yin-jingyu/archive/2011/08/01/2123548.html)
http回复
下面就是http回复的例子,除了这个图之外,后面的部分就是大家看到的页面的源代码
socket
我们一般说的socket,广义上包含java.net包下的Socket类与ServerSocekt类。
另一方面有基于tcp的网络编程也有基于udp的网络编程,其中差别大家百度,这里只谈tcp。
定义性的东西大家可以查看各种资料(个人推荐尚学堂 马士兵老师讲解的socket部分视频,但在看socket之前建议大家先看io部分),带大家看一段代码,大家应该就会知道socket编程的大致原理了。(代码来自马士兵老师的讲义)
import java.net.*; import java.io.*; public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); //在本机的TCP6666号端口上监听 while(true) { Socket s = ss.accept(); //ServerSocket的accept为阻塞式方法,只有当它监听到一个请求时 //它才会执行 System.out.println("a client connect!"); DataInputStream dis = new DataInputStream(s.getInputStream());//获得客户端向自己说的"话" //InputStream 从外部指向内存 System.out.println(dis.readUTF()); //按照uft-8的格式读取内容 dis.close(); s.close(); } } } import java.net.*; import java.io.*; public class TCPClient { public static void main(String[] args) throws Exception { Socket s = new Socket("127.0.0.1", 6666); //连接127.0.0.1(本机)的tcp端口6666 OutputStream os = s.getOutputStream(); //获得一条线路,来给服务器"说话" DataOutputStream dos = new DataOutputStream(os);//对这个线路进行包装 Thread.sleep(3000); //"暂停"3秒 dos.writeUTF("hello server!"); //对服务器说 hello server! dos.flush(); dos.close(); s.close(); } }
先运行server端,再运行client端。当运行client端后,控制台首先会打印a client connect!三秒之后会打印hello server!
模拟一个最最最基础的tomcat
import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddress; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.File; public class HttpServer { /** WEB_ROOT is the directory where our HTML and other files reside. * For this package, WEB_ROOT is the "webroot" directory under the working * directory. * The working directory is the location in the file system * from where the java command was invoked. */ public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; // shutdown command private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // the shutdown command received private boolean shutdown = 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")); //这个1是什么功能 参加 http://www.51cto.com/art/200702/40196_1.htm } catch (IOException e) { e.printStackTrace(); System.exit(1); } // Loop waiting for a request while (!shutdown) { //最开始的时候 shutdown为false 这段话会执行 Socket socket = null; InputStream input = null; OutputStream output = null; try { socket = serverSocket.accept(); //只有当有客户端请求时 它才会运行 阻塞式方法! System.out.println(new Date()+"AAAAAAAAAAAA"); input = socket.getInputStream(); //里面放的是客户端对服务器说的话 output = socket.getOutputStream();//这里面将要放的是服务器要对客户端说的话 // create Request object and parse Request request = new Request(input); request.parse(); //参见request // create Response object Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); // Close the socket socket.close(); //check if the previous URI is a shutdown command shutdown = request.getUri().equals(SHUTDOWN_COMMAND); } catch (Exception e) { e.printStackTrace(); continue; } } } }
import java.io.InputStream; import java.io.IOException; public class Request { private InputStream input; private String uri; public Request(InputStream input) { this.input = input; } public void parse() { // Read a set of characters from the socket StringBuffer request = new StringBuffer(2048); 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]); //将客户端的请求加到request(StringBuffer)中 } System.out.println("request 如下"); System.out.print(request.toString()); uri = parseUri(request.toString()); System.out.println("uri 如下 "+uri); } /** *看看System.out.println("uri 如下 "+uri); 就不用解释这个方法了 * **/ private String parseUri(String requestString) { 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; } public String getUri() { return uri; } }
import java.io.OutputStream; import java.io.IOException; import java.io.FileInputStream; import java.io.File; /* HTTP Response = Status-Line *(( general-header | response-header | entity-header ) CRLF) CRLF [ message-body ] Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ 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 { System.out.println(HttpServer.WEB_ROOT+" **** "+request.getUri()); File file = new File(HttpServer.WEB_ROOT, request.getUri()); //连接用户请求的"文件" if (file.exists()) { fis = new FileInputStream(file); int ch = fis.read(bytes, 0, BUFFER_SIZE); //把文件里的东西读出来放到bytes字符数组里 while (ch!=-1) { //把bytes数组里的东西放到要给客户端回复的流里面 output.write(bytes, 0, ch); ch = fis.read(bytes, 0, BUFFER_SIZE); } } else { //要是文件不存在 不解释 // file not found 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) { // thrown if cannot instantiate a File object System.out.println(e.toString() ); } finally { if (fis!=null) fis.close(); } } }
首先如果大家用的是Eclipse,那么没有任何问题,如果大家是用命令行的形式的话会存在一个问题,HttpServer与Response两个类相互依赖,先编译谁?
解决办法 cd到三个类的目录 然后 javac *.java
等启动HttpServer后
在浏览器输入localhost:8080/index.html
显示如下
明白了吧 我们把index.html放到D:\尚学堂 j2ee\javase\尚学堂科技_马士兵_J2SE_5.0_第01章_JAVA简介_源代码_及重要说明\java\Socket\no\WebContent 目录下
index.html内容如下
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
我是index
</body>
</html>
再次请求 截图如下
大家一定很奇怪,我为什么不用火狐或这个Chrome,找个EditPlus点过来点过去。
大家试试就知道了,火狐不知道因为什么原因,在地址栏敲回车后,会发出两次请求。结果就是报错。
再试试http://localhost:8080/SHUTDOWN
程序退出
how tomcat works 读书笔记(一)----------一个简单的web服务器的更多相关文章
- how tomcat works 读书笔记(二)----------一个简单的servlet容器
app1 (建议读者在看本章之前,先看how tomcat works 读书笔记(一)----------一个简单的web服务器 http://blog.csdn.net/dlf123321/arti ...
- how tomcat works 读书笔记(一)----------一个简单的webserver
http协议 若是两个人能正常的说话交流,那么他们间必然有一套统一的语言规则<在网络上server与client能交流也依赖与一套规则,它就是我们说的http规则(超文本传输协议Hypertex ...
- Tomcat剖析(一):一个简单的Web服务器
Tomcat剖析(一):一个简单的Web服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器 ...
- 《深度解析Tomcat》 第一章 一个简单的Web服务器
本章介绍Java Web服务器是如何运行的.从中可以知道Tomcat是如何工作的. 基于Java的Web服务器会使用java.net.Socket类和java.net.ServerSocket类这两个 ...
- 自己模拟的一个简单的web服务器
首先我为大家推荐一本书:How Tomcat Works.这本书讲的很详细的,虽然实际开发中我们并不会自己去写一个tomcat,但是对于了解Tomcat是如何工作的还是很有必要的. Servlet容器 ...
- 一个简单的Web服务器-支持Servlet请求
上接 一个简单的Web服务器-支持静态资源请求,这个服务器可以处理静态资源的请求,那么如何处理Servlet请求的呢? 判断是否是Servlet请求 首先Web服务器需要判断当前请求是否是Servle ...
- 一个简单的Web服务器-支持静态资源请求
目标 实现一个简单的Web服务器,能够根据HTTP请求的URL响应对应的静态资源,如果静态资源不存在则响应404. HttpServer 使用ServerSocket实现的一个服务器,request根 ...
- 自己动手模拟开发一个简单的Web服务器
开篇:每当我们将开发好的ASP.NET网站部署到IIS服务器中,在浏览器正常浏览页面时,可曾想过Web服务器是怎么工作的,其原理是什么?“纸上得来终觉浅,绝知此事要躬行”,于是我们自己模拟一个简单的W ...
- 一个简单的web服务器
写在前面 新的一年了,新的开始,打算重新看一遍asp.net本质论这本书,再重新认识一下,查漏补缺,认认真真的过一遍. 一个简单的web服务器 首先需要引入命名空间: System.Net,关于网络编 ...
随机推荐
- ORACLE时间日期格式使用总结(参考网上资料汇总)
Oracle时间日期操作 sysdate+(5/24/60/60) 在系统时间基础上延迟5秒 sysdate+5/24/60 在系统时间基础上延迟5分钟 sysdate+5/24 在系统时间基础上延迟 ...
- PGM:有向图模型:贝叶斯网络
http://blog.csdn.net/pipisorry/article/details/52489270 为什么用贝叶斯网络 联合分布的显式表示 Note: n个变量的联合分布,每个x对应两个值 ...
- Linux telnet远程登录操作
telnet (如果不行 可以却换root帐户试试 su - root) 1.安装telnet-server sudo dpkg -i xinetd_1%3a2.3.14-7ubuntu3_ ...
- emysql add_poop() 超时出错
emysql add_poop() 超时出错(金庆的专栏)sample/a_hello.erl 连接本机更改为连接局域网内的MySql服务器: emysql:add_pool(hello_poo ...
- Team Foundation Server 2015 Update 2.1 发布日志
微软在 2016年5月5日发布了Visual Studio Team Foundation Server 2015 update 2.1. 下面我们来看看Update2.1中给我们带来了哪些新功能. ...
- UNIX网络编程——epoll 系列函数简介、与select、poll 的区别
前面博客<<UNIX环境高级编程--epoll函数使用详解>>有关于epoll函数的讲解. 一.epoll 系列函数简介 #include <sys/epoll.h> ...
- 【JavaEE WEB 开发】Tomcat 详解 Servlet 入门
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...
- Android的View类介绍-android的学习之旅(十三)
view概述 android绝大部分UI组件都放在android.view和android.widght包中,android的虽有UI组件都继承了View类. View类还有一个非常重要的子类:Vie ...
- 05 Android强制设置横屏或竖屏/全屏
全屏 在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码 requestWindowFeature(Window.FEATURE_NO_TIT ...
- 5.创建表,使用alter进行表信息的增删改,Oracle回收站,集合运算
1 Oracle基于用户的管理方案 2 DDL语句可以管理数据库的对象有:视图 索引 序列 同义词 约束 3 创建一个表,有2个条件(1 有权限:2有表空间) Oracle给你提 ...