设为首页 加入收藏

TOP

how tomcat works 读书笔记(二)----------一个简单的servlet容器(四)
2015-07-20 17:26:47 来源: 作者: 【 】 浏览:18
Tags:how tomcat works 读书 笔记 ---------- 一个 简单 servlet 容器
1 = requestString.indexOf(' '); if (index1 != -1) { index2 = requestString.indexOf(' ', index1 + 1); if (index2 > index1) return requestString.substring(index1 + 1, index2); } return null; } public void parse() { // Read a set of characters from the socket 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
同样的response也要实现ServletResponse接口
package ex02.pyrmont;


import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Locale;
import javax.servlet.ServletResponse;
import javax.servlet.ServletOutputStream;


import com.sun.org.apache.bcel.internal.generic.NEW;


public class Response implements ServletResponse {


  private static final int BUFFER_SIZE = 1024;
  Request request;
  OutputStream output;
  PrintWriter writer;


  public Response(OutputStream output) {
    this.output = output;
  }


  public void setRequest(Request request) {
    this.request = request;
  }


  /* This method is used to serve a static page */
  public void sendStaticResource() throws IOException {
    byte[] bytes = new byte[BUFFER_SIZE];
    FileInputStream fis = null;
    try {
      /* request.getUri has been replaced by request.getRequestURI */
    	System.out.println(Constants.WEB_ROOT+" ***** "+ request.getUri());
      File file = new File(Constants.WEB_ROOT, request.getUri());
      fis = new FileInputStream(file);
      /*
         HTTP Response = Status-Line
           *(( general-header | response-header | entity-header ) CRLF)
           CRLF
           [ message-body ]
         Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
      */
      int ch = fis.read(bytes, 0, BUFFER_SIZE);
      System.out.println("response  **");
      while (ch!=-1) {
        output.write(bytes, 0, ch);
        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" +
        "

File Not Found

"; output.write(errorMessage.getBytes()); } finally { if (fis!=null) fis.close(); } } public PrintWriter getWriter() throws IOException { // autoflush is true, println() will flush, // but print() will not. writer = new PrintWriter(output, true); return writer; } //一样 部分代码 直接让Eclipse 自动生成即可 }


试验一下
http://localhost:8080/servlet/PrimitiveServlet
\


控制台显示

GET /servlet/PrimitiveServlet HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash,
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1)
Accept-Encoding: gzip, deflate
Host: localhost:8080
Connection: Keep-Alive


PrimitiveServlet*****D:\Java Code\UpLoad\webroot file:D:\Java Code\UpLoad\webroot\
from service



大家仔细看看
Violets are blue. 这句话没有打印出来
大家看看Response类getWriter方法里的PrintWriter一行
上面的英文,我就不解释了
这个bug在以后的版本中会修改




app2

上面的代码其实还存在一个问题
在request部分中parseUri是private的,在这一节中这样其实是可以的,但问题是这个方法有可能在外部类中使用,应该是public的。
如果改成public,问题又出现了
  public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException {
    System.out.println("from service");
    Request r=(Request) request;
    System.out.println(r.par
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA11504- Dominos(Tarjan+缩点) 下一篇HDU 4849-Wow! Such City!(最短..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)