tip:由于作为调试有一些多余的System.out.println()
语句,请忽略。
模拟客户端发送请求,并接收服务器返回的数据:
package com.milk.mytomcat.one;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/*
模拟客户端发送请求,并接受服务端传回来的数据
*/
public class TestClient {
public static void main(String[] args) throws Exception {
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
socket = new Socket("www.faw.com.cn", 80);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
outputStream.write("GET /fawcn/373694/373706/index.html HTTP/1.1\n".getBytes());
outputStream.write("HOST:www.faw.com.cn\n".getBytes());
outputStream.write("\n".getBytes());
int i = inputStream.read();
while (i != -1) {
System.out.print((char)i);
i = inputStream.read();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != inputStream) {
inputStream.close();
inputStream = null;
}
if (null != outputStream) {
outputStream.close();
outputStream = null;
}
if(null != socket) {
socket.close();
socket = null;
}
}
}
}
模拟服务端向客户端响应数据
package com.milk.mytomcat.one;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
模拟服务端向客户端响应数据
*/
public class TestServer {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = null;
Socket socket = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(10086);
socket = serverSocket.accept();
outputStream = socket.getOutputStream();
outputStream.write("HTTP/1.1 200 OK\n".getBytes());
outputStream.write("Content-Type:text/html;charset=utf-8\n".getBytes());
outputStream.write("Server:Apache-Coyote/1.1\n".getBytes());
outputStream.write("\n\n".getBytes());
StringBuffer buffer = new StringBuffer();
buffer.append("");
buffer.append("I am Title ");
buffer.append("");
buffer.append("I am header 1
");
buffer.append("百度");
buffer.append("");
buffer.append("");
outputStream.write(buffer.toString().getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != outputStream) {
outputStream.close();
outputStream = null;
}
if (null != socket) {
socket.close();
socket = null;
}
}
}
}
Tomcat版本1:响应静态页面
package com.milk.mytomcat.two;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/*
Tomcat版本1
*/
public class Tomcat01 {
public static String WEB_ROOT = System.getProperty("user.dir") + "\\" + "WebContent";
private static String url = "";
public static void main(String[] args) throws Exception {
// System.out.println(WEB_ROOT);
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(10086);
while(true) {
// 接受客户端发送过来的请求
socket = serverSocket.accept();
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
parse(inputStream);
sendStaticResource(outputStream);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != outputStream) {
outputStream.close();
outputStream = null;
}
if(null != inputStream) {
inputStream.close();
inputStream = null;
}
if(null != socket) {
socket.close();
socket = null;
}
}
}
// 发送静态资源
private static void sendStaticResource(OutputStream outputStream) throws Exception {
byte[] bytes = new byte[2048];
FileInputStream fileInputStream = null;
File file = new File(WEB_ROOT, url);
// 如果文件存在
System.out.println(file.getAbsoluteFile());
try {
if (file.exists()) {
System.out.println("文件存在");
outputStream.write("HTTP/1.1 200 OK\n".getBytes());
outputStream.write("Server:apache-Coyote/1.1\n".getBytes());
outputStream.write("Content-Type:text/html;charset=utf-8\n".getBytes());
outputStream.write("\n".getBytes());
fileInputStream = new FileInputStream(file);
int ch = fileInputStream.read(bytes);
while (ch != -1) {
outputStream.write(bytes, 0, ch);
ch = fileInputStream.read(bytes);
}
} else { // 如果文件不存在
System.out.println("文件不存在");
outputStream.write("HTTP/1.1 404 not found\n".getBytes());
outputStream.write("Server:apache-Coyote/1.1\n".getBytes());
outputStream.write("Content-Type:text/html;charset=utf-8\n".getBytes());
outputStream.write("\n".getBytes());
String errorMessage = "file not found";
outputStream.write(errorMessage.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fileInputStream) {
fileInputStream.close();
}
}
}
private static void parse(InputStream inputStream) throws Exception {
// StringBuffer content = new StringBuffer(2048);
byte[] buffer = new byte[2048];
int i = -1;
i = inputStream.read(buffer);
// for (int j = 0; j < i; j ++) {
// content.append((char)buffer[j]);
// }
String content = new String(buffer);
System.out.println(content);
parseUrl(content.toString());
}
private static void parseUrl(String content) {
int index1,index2;
index1 = content.indexOf(" ");
if (index1 != 1) {
index2 = content.indexOf(" ", index1 + 1);
if (index2 > index1) {
url = content.substring(index1 + 2, index2);
}
}
System.out.println(url);
}
}
Tomcat版本二:响应动态资源
package com.milk.mytomcat.two;
import com.milk.mytomcat.two.servlet.api.Servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
/*
扩展内容
查看端口占用进程并关闭
netstat -aon|findstr "8080"
tasklist|findstr "14248"
taskkill /f /t /im node.exe
*/
/*
Tomcat版本2
*/
public class Tomcat02 {
public static String WEB_ROOT = System.getProperty("user.dir") + "\\" + "WebContent";
private static String url = "";
// 用来保存配置文件的映射
private static Map map = new HashMap<>();
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(WEB_ROOT + "\\conf.properties")); // 读取配置文件
Set
准备两个Servlet作为测试
先准备Servlet接口
package com.milk.mytomcat.two.servlet.api;
import java.io.InputStream;
import java.io.OutputStream;
public interface Servlet {
void init();
void Service(InputStream inputStream, OutputStream outputStream);
void destroy();
}
AAServlet .java
package com.milk.mytomcat.two.servlet.impl;
import com.milk.mytomcat.two.servlet.api.Servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class AAServlet implements Servlet {
@Override
public void init() {
System.out.println("aa...init...");
}
@Override
public void Service(InputStream inputStream, OutputStream outputStream) {
System.out.println("AA service");
try {
outputStream.write("I am from Server..AA".getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
System.out.println("aa.....destroy....");
}
}
BBServlet .java
package com.milk.mytomcat.two.servlet.impl;
import com.milk.mytomcat.two.servlet.api.Servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class BBServlet implements Servlet {
@Override
public void init() {
System.out.println("bb...init...");
}
@Override
public void Service(InputStream inputStream, OutputStream outputStream) {
System.out.println("bbServlet...service");
try {
outputStream.write("I am from BBServlet".getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void destroy() {
System.out.println("BBServlet Destroy");
}
}
配置文件
conf.properties
,相当于web.xml
,作为路径的映射。
当然,这里只是为了简略,也可以换成使用dom4j来解析xml
aa=com.milk.mytomcat.two.servlet.impl.AAServlet
bb=com.milk.mytomcat.two.servlet.impl.BBServlet