A Simple NIO-based HTTP/HTTPS Server Example
INTRODUCTION
============
This directory contains a simple HTTP/HTTPS server. HTTP/HTTPS are two
common network protocols that provide for data transfer, and are more
fully described in RFC 2616 and RFC 2818 (Available at
http://www.ietf.org ). HTTPS is essentially HTTP after the connection
has been secured with SSL/TLS. TLS is the successor to SSL, and is
described in RFC 2246.
This server was written to demonstrate some of the functionality new to
the Java 2 platform. The demo is not meant to be a full tutorial, and
assumes the reader has some familiarity with the subject matter.
In particular, it shows:
New I/O (java.nio, java.nio.channels, java.util.regex, java.nio.charset)
Introduced in version 1.4 of the platform, NIO was designed to
overcome some of the scalability limitations found in the
existing blocking java.net.* API's, and to address other
concepts such as Regular Expression parsing and Character
Sets.
This server demonstrates:
ByteBuffer
Blocking and Non-Blocking I/O
SocketChannel
ServerSocketChannel
Selector
CharacterSet
Pattern matching using Regular Expressions
JSSE (javax.net.ssl)
Introduced in version 1.4 of the platform, JSSE provides
network security using SSL/TLS for java.net.Socket-based
traffic. In version 1.5, the SSLEngine API was introduced
which separates the SSL/TLS functionality from the underlying
I/O model. By making this separation, applications can adapt
I/O and compute strategies to best fit their circumstances.
This server demonstrates:
Using SSLEngine to create a HTTPS server
Creating simple key material for use with HTTPS
Concurrency Library (java.util.concurrent)
Introduced in version 1.5 of the platform, the concurrency
library provides a mechanism which decouples task submission
from the mechanics of how each task will be run.
This server demonstrates:
A ThreadPool with a fixed number of threads, which is
based on the number of available processors.
SETUP
=====
The server must be built on version 1.5 (or later) of the platform.
Invoking the following should be sufficient:
% mkdir build
% javac -source 1.5 -target 1.5 -d build *.java
The following creates the document root:
% mkdir root
All documents should be placed in this directory.
For HTTPS, the server authenticates itself to clients by using simple
Public Key Infrastructure (PKI) credentials in the form of
X509Certificates. You must create the server's credentials before
attempting to run the server in "-secure" mode. The server is
currently hardcoded to look for its credentials in a file called
"testkeys".
In this example, we'll create credentials for a fictional widget web
site owned by the ubiquitous "Xyzzy, Inc.". When you run this in your
own environment, replace "widgets.xyzzy.com" with the hostname of your
server.
The easiest way to create the SSL/TLS credentials is to use the
java keytool, by doing the following:
(<CR> represents your end-of-line key)
% keytool -genkey -keyalg rsa -keystore testkeys -alias widgets
Enter keystore password: passphrase
What is your first and last name?
[Unknown]: widgets.xyzzy.com<CR>
What is the name of your organizational unit?
[Unknown]: Consumer Widgets Group<CR>
What is the name of your organization?
[Unknown]: Xyzzy, Inc.<CR>
What is the name of your City or Locality?
[Unknown]: Arcata<CR>
What is the name of your State or Province?
[Unknown]: CA<CR>
What is the two-letter country code for this unit?
[Unknown]: US<CR>
Is CN=widgets.xyzzy.com, OU=Consumer Widgets Group, O="Xyzzy, Inc.",
L=Arcata, ST=CA, C=US correct?
[no]: yes<CR>
Enter key password for <mykey>
(RETURN if same as keystore password): <CR>
This directory also contain a very simple URL reader (URLDumper), which
connects to a specified URL and places all output into a specified file.
SERVER EXECUTION
================
% java -classpath build Server N1
Usage: Server <type> [options]
type:
B1 Blocking/Single-threaded Server
BN Blocking/Multi-threaded Server
BP Blocking/Pooled-thread Server
N1 Nonblocking/Single-threaded Server
N2 Nonblocking/Dual-threaded Server
options:
-port port port number
default: 8000
-backlog backlog backlog
default: 1024
-secure encrypt with SSL/TLS
default is insecure
"http://" URLs should be used with insecure mode, and
"https://" for secure mode.
The "B*" servers use classic blocking I/O: in other words, calls to
read()/write() will not return until the I/O operation has completed. The
"N*" servers use non-blocking mode and Selectors to determine which
Channels are ready to perform I/O.
B1: A single-threaded server which completely services each
connection before moving to the next.
B2: A multi-threaded server which creates a new thread for each
connection. This is not efficient for large numbers of
connections.
BP: A multi-threaded server which creates a pool of threads for use
by the server. The Thread pool decides how to schedule those
threads.
N1: A single-threaded server. All accept() and read()/write()
operations are performed by a single thread, but only after
being selected for those operations by a Selector.
N2: A dual-threaded server which performs accept()s in one thread, and
services requests in a second. Both threads use select().
CLIENT EXECUTION
================
You can test the server using any standard browser such as Internet
Explorer or Mozilla, but since the browser will not trust the
credentials you just created, you may need to accept the credentials
via the browser's pop-up dialog box.
Alternatively, to use the certificates using the simple included JSSE
client URLDumper, export the server certificate into a new truststore,
and then run the application using the new truststore.
% keytool -export -keystore testkeys -alias widgets -file widgets.cer
Enter keystore password: passphrase<CR>
Certificate stored in file <widgets.cer>
% keytool -import -keystore trustCerts -alias widgetServer \
-file widgets.cer
Enter keystore password: passphrase<CR>
Owner: CN=widgets.xyzzy.com, OU=Consumer, O="xyzzy, inc.", L=Arcata,
ST=CA, C=US
Issuer: CN=widgets.xyzzy.com, OU=Consumer, O="xyzzy, inc.",
L=Arcata, ST=CA, C=US
Serial number: 4086cc7a
Valid from: Wed Apr 21 12:33:14 PDT 2004 until: Tue Jul 20 12:33:14
PDT 2004
Certificate fingerprints:
MD5: 39:71:42:CD:BF:0D:A9:8C:FB:8B:4A:CD:F8:6D:19:1F
SHA1: 69:5D:38:E9:F4:6C:E5:A7:4C:EA:45:8E:FB:3E:F3:9A:84:01:6F:22
Trust this certificate? [no]: yes<CR>
Certificate was added to keystore
% java -classpath build -Djavax.net.ssl.trustStore=trustCerts \
-Djavax.net.ssl.TrustStorePassword=passphrase \
URLDumper https://widgets.xyzzy.com:8000/ outputFile
NOTE: The server must be run with "-secure" in order to receive
"https://" URLs.
WARNING: This is just a simple example for code exposition, you should
spend more time understanding PKI security concerns.
SOURCE CODE OVERVIEW
====================
The main class is Server, which handles program startup, and is
subclassed by the "B*" and "N*" server classes.
Following a successful accept(), the "B*" variants each create a
RequestServicer object to perform the actual request/reply operations. The
primary differences between the different "B*" servers is how the
RequestServicer is actually run:
B1
JDK1.8 windows zip解压缩版
需积分: 0 127 浏览量
更新于2023-07-26
收藏 120.98MB ZIP 举报
**正文**
JDK1.8 Windows Zip解压缩版是一款专为Windows操作系统设计的Java Development Kit(Java开发工具包)的非安装版本,它以ZIP压缩格式提供,方便用户下载后直接解压使用,无需经过传统的安装过程。这个版本特别适合那些不希望在系统注册表中留下痕迹或者对系统权限有限制的用户,同时也简化了开发环境的搭建。
JDK是Java语言的核心组成部分,包含了Java编译器、运行时环境(JRE)、调试工具、文档以及各种API。JDK1.8是Java历史上的一个重要版本,它引入了许多新特性和优化,极大地提升了开发效率和程序性能。
1. **新特性**
- **Lambda表达式**:JDK1.8引入了Lambda表达式,使得函数式编程成为可能,简化了多线程处理和集合操作。
- **Stream API**:Stream API允许对集合进行高效且富有表达性的并行操作,极大地增强了数据处理能力。
- **方法引用**:允许直接引用类的方法,减少代码冗余,提高可读性。
- **默认方法**:在接口中可以定义带有实现的方法,增强了接口的功能性。
- **Date和Time API**:进行了全面的重构,提供了更强大、更易用的时间日期处理功能。
2. **性能优化**
- **G1垃圾收集器**:作为默认的垃圾收集器,G1提供了更好的内存管理,减少了应用暂停时间。
- ** invokedynamic指令**:提高了动态语言的执行效率,对于脚本语言和反射调用有显著提升。
3. **开发者工具**
- **JShell**:新增的即时编译器(REPL,Read-Eval-Print Loop),方便开发者快速测试代码片段。
- **改进的JConsole** 和 **VisualVM**:提供更好的性能监控和分析工具,帮助开发者优化应用程序。
4. **安全性**
- JDK1.8在安全方面也进行了加强,包括证书管理和加密算法的更新,提高了系统的安全性。
5. **JavaFX**
- JDK1.8包含了JavaFX,这是一个用于创建桌面和移动设备的富客户端应用程序的平台,支持2D和3D图形,以及媒体播放。
使用JDK1.8 Windows Zip解压缩版,用户只需将压缩包解压到指定目录,设置好环境变量(如JAVA_HOME、PATH),即可在命令行中使用javac进行编译,java命令运行程序。对于开发人员来说,这是一个快速启动Java开发的便捷方式。
JDK1.8 Windows Zip解压缩版为Windows用户提供了一个轻量级且功能强大的Java开发环境,它的引入和特性不仅提高了开发效率,还为Java编程带来了新的变革。无论是初学者还是经验丰富的开发者,都能从中受益。

SS上善
- 粉丝: 130
- 资源: 7
最新资源
- (源码)基于Arduino的机器人玩具狗项目.zip
- 基于JavaScript的智慧物业管理系统设计源码
- 基于博途1200PLC与组态王的起重机仿真控制系统的设计与实现,基于博途1200PLC与组态王的起重机仿真控制系统设计与实现,基于博途1200PLC和组态王的起重机仿真控制系统 ,基于博途1200P
- 基于PHP、JavaScript、HTML的博物馆API设计源码
- (源码)基于jQuery的脚手架配置项目.zip
- 基于SpringBoot和Vue的现代化学生选课系统设计源码
- 基于Vue、JavaScript、HTML的数字化企业管理平台设计源码
- 基于博途1200PLC的智能彩色广告屏流水灯仿真系统设计与实现,基于博途1200PLC技术的先进彩色广告屏流水灯仿真系统设计与实现,No.109.基于博途1200PLC的新型彩色广告屏流水灯仿真系统
- (源码)基于Arduino的家用时钟系统.zip
- 基于微信小程序的图书馆自习室预约系统设计源码
- 基于Java语言的small_dragon_console_system_rbac权限控制设计源码
- 模糊自适应PID控制策略在微电网母线电压稳定控制中的应用:通过精确调节自适应PID参数,实现母线电压的稳定控制程序,基于模糊自适应PID算法的微电网母线电压稳定控制策略与程序实现,模糊自适应PID对微
- (源码)基于嵌入式C语言的物联网设备监控系统.zip
- 博途1200 PLC驱动的邮件高效分拣控制系统的设计与实现:V15软件版本探索,基于博途1200 PLC的邮件分拣控制系统软件V15的设计与实现,基于博途1200 plc的邮件分拣控制系统 软件版本:
- (源码)基于Arduino和PlatformIO的Tony Space IoT教学样板.zip
- MMC模块化多电平换流器仿真研究:7电平闭环控制下的直流电压与有功无功平衡调节技术,含电容电压平衡与环流抑制策略,载波移相调制技术实践参考,MMC模块化多电平换流器仿真研究:包括闭环控制、直流电压外环