第一章 一个简单的Web服务器

该应用程序仅接受位于指定目录的静态资源的请求,如HTML文件和图像文件。它也可以将传入的HTTP请求字节流显示到控制台上。但是,它并不发送任何头信息到浏览器,如日期或者cookies等。

应用程序的入口在HttpServer的静态main方法中,main()方法会创建一个HttpServer实例。然后,调用其await()方法,顾名思义,await()方法就是在指定端口上等待HTTP请求,对其进行处理,然后发送响应信息回客户端,在接受到关闭命令之前,它会保持等待状态。

HttpServer.java

package com.simpleHttpServer;

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
{ public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; // 关闭HttpServer命令
private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // 是否收到关闭HttpServer命令
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"));
} catch (IOException e)
{
e.printStackTrace();
System.exit(1);
} // 循环等待Http请求
while (!shutdown)
{
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try
{
socket = serverSocket.accept();
input = socket.getInputStream();
output = socket.getOutputStream(); // 创建一个Request对象并解析
Request request = new Request(input);
request.parse(); // 创建一个Response对象
Response response = new Response(output);
response.setRequest(request);
response.sendStaticResource(); // 关闭socket
socket.close(); // 检查是否是关闭命令
shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
} catch (Exception e)
{
e.printStackTrace();
continue;
}
}
}
}

Request.java 

package com.simpleHttpServer;

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()
{
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]);
}
System.out.print(request.toString());
uri = parseUri(request.toString());
} 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;
} }

Response.java

package com.simpleHttpServer;

import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File; 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());
if (file.exists())
{
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch != -1)
{
output.write(bytes, 0, ch);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} else
{
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)
{
System.out.println(e.toString());
} finally
{
if (fis != null)
fis.close();
}
}
}

 

(1)System.getProperty("user.dir")是取得当前工作目录

HttpServer中定义的public static final String WEB_ROOT 即     工作目录/webroot

(2) 在创建TCP服务端监听的时候

try
{
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
} catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}

程序主动捕获了IOException,深入ServerSocket源码可以看到:

public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
setImpl();
if (port < 0 || port > 0xFFFF)
throw new IllegalArgumentException(
"Port value out of range: " + port);
if (backlog < 1)
backlog = 50;
try {
bind(new InetSocketAddress(bindAddr, port), backlog);
} catch(SecurityException e) {
close();
throw e;
} catch(IOException e) {
close();
throw e;
}
}

  在创建ServerSocket对象(开启TCP服务端监听)的时候可能抛出三种异常

(1) new IllegalArgumentException  在要监听的TCP端口范围不在正确范围0-65535之间,就抛出参数异常 改异常时候RuntimeException 不用显式在方法后throws以及方法调用处try catch

(2) SecurityException 也是一个RuntimeException  异常触发条件, 如果安全管理器存在并且其 checkListen 方法不允许进行该操作。

(3) IOException 是非运行时异常  如果打开套接字时发生 I/O 错误。

简要介绍下ServerSocket:

把服务器套接字绑定到特定的端口号,这样远程客户端才能定位TCP服务,如果传递进来的值为零(zero),就使用任何空闲的端口--但是客户端可能没办法访问该服务,除非用什么方式通知了客户端端口号是多少,为队列分配足够的空间以支 持特定数量的客户端套接字。在ServerSocket(int port, int numberOfClients)构造函数的重载版本 中,加入了InetAddress参数,在多地址计算机上,它允许服务器套接字绑定到某个特定的IP地址。例如,某台计算机可能有两块网 卡,或者使用虚拟IP地址把它配置成像几台计算机一样工作的时候。如果地址的值为空(null),服务 器套接字将在所有的本地地址上接受请求。在默认情况下,队列的大小设置为50,但是也提供了备用的构造函数,它允许修改这个设置。如果端口已经被绑定了,或者安全性约束条件(例如安全性规则或知名端口上的操作系统约束条件)阻挡了访问,就会产生异常。

(3) 程序创建File对象,调用了构造函数,java.io.File类中不常用的一个构造方法

根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
如果 parent 为 null,则创建一个新的 File 实例,这与调用以给定 child 路径名字符串作为参数的单参数 File 构造方法效果一样。 否则,parent 路径名字符串用于表示目录,child 路径名字符串用于表示目录或文件。如果 child 路径名字符串是绝对路径名,
则用与系统有关的方式将它转换为一个相对路径名。如果 parent 是空字符串,则通过将 child 转换为抽象路径名,
并根据与系统有关的默认目录解析结果来创建新的 File 实例。否则,将每个路径名字符串转换为一个抽象路径名,并根据父抽象路径名解析子抽象路径名。 参数:
parent - 父路径名字符串
child - 子路径名字符串
抛出:
NullPointerException - 如果 child 为 null

  

 public File(String parent, String child) {
if (child == null) {
throw new NullPointerException();
}
if (parent != null) {
if (parent.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
this.path = fs.resolve(fs.normalize(parent),
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}

  

最新文章

  1. Emotion Debt 2017/1/6
  2. 属性字符串的replaceCharactersInRange方法
  3. 能源项目xml文件 -- springMVC-servlet.xml -- context:component-scan
  4. IIS7.5真变态,服务器时间格式导致不生成WebResource.axd
  5. UI学习笔记---第十三天可视化设计 XIB, StoryBoard
  6. MySql拾遗
  7. [code]字母重排
  8. 使用Hibernate Tools从数据库逆向生成Hibernate实体类
  9. VINS 估计器之结构初始化
  10. Spring之旅第三篇-Spring配置详解
  11. Django之URL路由系统
  12. 随机数Random
  13. js对象深拷贝与浅拷贝
  14. csharp: FTP Client Library using System.Net.FtpClient and FluentFTP,测试中存在的BUG修正
  15. diff命令详解
  16. 禁用SSL v2.0、SSL v3.0协议
  17. java实现在图片上编辑文本内容
  18. 1.js基础(以通俗易懂的语言解释JavaScript)
  19. Educational Codeforces Round 26 D dp
  20. hbase shell学习-2

热门文章

  1. 驱动lx4f120h,头文件配置,没有完全吃透,望指点
  2. java小算法—大衍数列
  3. cocos2dx游戏项目总结(持续更新)
  4. pugixml
  5. Paxos算法之旅(四)zookeeper代码解析--转载
  6. Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View
  7. JavaScript 是世界上最好的语言?
  8. Base64算法
  9. springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
  10. volatile用处说明