Giter Site home page Giter Site logo

simpledwebcontainer's Introduction

simpledwebcontainer's People

Contributors

outliwer avatar

Stargazers

Jack avatar lxw avatar  avatar  avatar Derrick Wang avatar

simpledwebcontainer's Issues

对于目前静态网页返回无法正常显示

  • 当前问题

    • 当前完成了对传递过来的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的加载首先需要的就是自定义一个网络应用类加载器
  • 应该拥有的功能
    • 设置库
    • 设置类路径
    • 设置访问权限
    • 开启自动重载线程
    • 缓存
    • 加载类

一个`servlet`正常的访问实现

  • 在继承javax包中的servlet
  • 可以较为清晰的看到servlet的生命周期,这里仅仅来探讨service实现的机制
  • 问题:怎么在service完成一个servlet类对servletRequestservletResponse的处理
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() {

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.