outliwer / simpledwebcontainer Goto Github PK
View Code? Open in Web Editor NEW自己手动开始编写一个简易的类似于Tomcat的Web容器
自己手动开始编写一个简易的类似于Tomcat的Web容器
当前问题
当前完成了对传递过来的httprequest
流的解析,目前在分析其为访问静态资源的时候出现的问题
对于不存在的静态资源会显示出现404
的界面,但是对于已经存在的静态文件无法显示正确的网页界面
分析原因
通过断点的方式查找出原因:对于无法寻找的情况自己已经填补了httpresponse
的返回头等,而对于存在的文件目前只是在流中写入了文件的内容,没有返回相关的头部参数等
try {
fis = new FileInputStream(file);
int messageLength = 0;
String fileName = file.getName();
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
messageLength += ch;
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
// get the content
fis = new FileInputStream(file);
ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1){
output.write(bytes);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} catch (FileNotFoundException e) {
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());
} finally {
if (fis!=null) {
fis.close();
}
}
解决方法
增加一个用于记录的hashmap
的对象头参数
/**
* the headers of the response
*/
private HashMap headers = new HashMap();
在填充文本内容前增加对将要返回内容的解析
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
当前问题
当前完成了对传递过来的httprequest
流的解析,目前在分析其为访问静态资源的时候出现的问题
对于不存在的静态资源会显示出现404
的界面,但是对于已经存在的静态文件无法显示正确的网页界面
分析原因
通过断点的方式查找出原因:对于无法寻找的情况自己已经填补了httpresponse
的返回头等,而对于存在的文件目前只是在流中写入了文件的内容,没有返回相关的头部参数等
try {
fis = new FileInputStream(file);
int messageLength = 0;
String fileName = file.getName();
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
messageLength += ch;
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
// get the content
fis = new FileInputStream(file);
ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1){
output.write(bytes);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} catch (FileNotFoundException e) {
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());
} finally {
if (fis!=null) {
fis.close();
}
}
解决方法
增加一个用于记录的hashmap
的对象头参数
/**
* the headers of the response
*/
private HashMap headers = new HashMap();
在填充文本内容前增加对将要返回内容的解析
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
当前问题
当前完成了对传递过来的httprequest
流的解析,目前在分析其为访问静态资源的时候出现的问题
对于不存在的静态资源会显示出现404
的界面,但是对于已经存在的静态文件无法显示正确的网页界面
分析原因
通过断点的方式查找出原因:对于无法寻找的情况自己已经填补了httpresponse
的返回头等,而对于存在的文件目前只是在流中写入了文件的内容,没有返回相关的头部参数等
try {
fis = new FileInputStream(file);
int messageLength = 0;
String fileName = file.getName();
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
messageLength += ch;
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
// get the content
fis = new FileInputStream(file);
ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1){
output.write(bytes);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} catch (FileNotFoundException e) {
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());
} finally {
if (fis!=null) {
fis.close();
}
}
解决方法
增加一个用于记录的hashmap
的对象头参数
/**
* the headers of the response
*/
private HashMap headers = new HashMap();
在填充文本内容前增加对将要返回内容的解析
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
当前问题
当前完成了对传递过来的httprequest
流的解析,目前在分析其为访问静态资源的时候出现的问题
对于不存在的静态资源会显示出现404
的界面,但是对于已经存在的静态文件无法显示正确的网页界面
分析原因
通过断点的方式查找出原因:对于无法寻找的情况自己已经填补了httpresponse
的返回头等,而对于存在的文件目前只是在流中写入了文件的内容,没有返回相关的头部参数等
try {
fis = new FileInputStream(file);
int messageLength = 0;
String fileName = file.getName();
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
messageLength += ch;
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
// get the content
fis = new FileInputStream(file);
ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1){
output.write(bytes);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} catch (FileNotFoundException e) {
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());
} finally {
if (fis!=null) {
fis.close();
}
}
解决方法
增加一个用于记录的hashmap
的对象头参数
/**
* the headers of the response
*/
private HashMap headers = new HashMap();
在填充文本内容前增加对将要返回内容的解析
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
当前问题
当前完成了对传递过来的httprequest
流的解析,目前在分析其为访问静态资源的时候出现的问题
对于不存在的静态资源会显示出现404
的界面,但是对于已经存在的静态文件无法显示正确的网页界面
分析原因
通过断点的方式查找出原因:对于无法寻找的情况自己已经填补了httpresponse
的返回头等,而对于存在的文件目前只是在流中写入了文件的内容,没有返回相关的头部参数等
try {
fis = new FileInputStream(file);
int messageLength = 0;
String fileName = file.getName();
int ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1) {
messageLength += ch;
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
// get the content
fis = new FileInputStream(file);
ch = fis.read(bytes, 0, BUFFER_SIZE);
while (ch!=-1){
output.write(bytes);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
} catch (FileNotFoundException e) {
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());
} finally {
if (fis!=null) {
fis.close();
}
}
解决方法
增加一个用于记录的hashmap
的对象头参数
/**
* the headers of the response
*/
private HashMap headers = new HashMap();
在填充文本内容前增加对将要返回内容的解析
setHeaders("content-length",String.valueOf(messageLength));
setHeaders("content-type", ContentTypeFind.findTheType(fileName));
// send the header
sendHeaders();
Servlet
的时候要进行Servlet
的加载Servlet
的加载首先需要的就是自定义一个网络应用类加载器此前对于静态文件显示使用了直接向输出流里面写请求头的方式,但后来动态加载Servlet时候返回头没有指定返回的数目也可以进行返回
javax
包中的servlet
类servlet
的生命周期,这里仅仅来探讨service
实现的机制service
完成一个servlet
类对servletRequest
和servletResponse
的处理public void init(ServletConfig servletConfig) throws ServletException {
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
public String getServletInfo() {
return null;
}
public void destroy() {
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.