欢迎光临极品网,更多、更新的资源信息尽在Jpinw.Com!本站所有信息资源每日更新新的内容,请大家继续关注www.Jpinw.com!如果觉得本站还不错,对您有帮助,别忘了向您的朋友推荐本站!请记好本站网址:http://www.Jpinw.com,网站发展靠大家多多的支持!!!

极品网 极品课件 极品论文 极品文学 极品游戏 极品美容 极品手机资源 极品股票

#
免费资源: 免费域名 | 免费空间 | 免费推广 | 免费邮箱 | 免费硬盘 | 免费论坛 | 免费留言 | 免费统计 | 在线投稿 | 更多...
电脑学院: 操作系统 | 安全相关 | 网页设计 | 编程开发 | 建站经验 | 服务器类 | 黑客攻防 | 菜鸟入门 | 教你网赚 | 更多...
文章导航: 网赚学堂 | 网赚秘笈 | 网赚三维 | 网赚先锋 | 网赚资讯 | 感悟网赚 | 众生百态 | 经典美文 | 范文中心 | 更多...
图酷天下: 时事图酷 | 娱乐图酷 | 搞笑图酷 | 时尚图酷 | 体育图酷 | 另类经典 | 论文资源 | 课件下载 | 文学知识 | 更多...

您现在的位置: 极品网 >> 电脑学院 >> 服务器类 >> WIN服务器 >> 教程正文

基于Jave的Web服务工作机制(6)          【字体:
基于Jave的Web服务工作机制(6)
作者:佚名    教程来源:不详    点击数:    更新时间:2006-6-2    

 








parseUri 方法从请求行那里得到URI。Listing 1.3 展示了parseUri 方法的用途。 parseUri 减缩请求中的第一个和第二个空格来获得URI。

  Listing 1.3. The Request class' parseUri method

  private String parseUri(String requestString) {
  int index1, index2;
  index1 = requestString.indexOf(' ');

  if (index1 != -1) {
    index2 = requestString.indexOf(' ', index1 + 1);
    if (index2 > index1)
     return requestString.substring(index1 + 1, index2);
  }

  return null;
}

  Response类

  Response表示一个HTTP响应。它的构造函数接受一个OutputStream对象,比如下面的:

  public Response(OutputStream output) {
  this.output = output;
  }
  Response 对象被HttpServer类的await方法构造,该方法被传递的参数是从socket那里得到的OutputStream对象。

  Response类有两个公共方法: setRequest和sendStaticResource. setRequest方法传递一个Request对象给Response对象。Listing 1.4中的代码显示了这个:

  Listing 1.4. The Response class' setRequest method

  public void setRequest(Request request) {
  this.request = request;
  }
  sendStaticResource 方法用来发送一个静态资源,比如HTML文件。Listing 1.5给出了它的实现过程:

  Listing 1.5. The Response class' sendStaticResource method

  public void sendStaticResource() throws IOException {
  byte[] bytes    = new byte[BUFFER_SIZE];
  FileInputStream fis = null;

  try {
    File file = new File(HttpServer.WEB_ROOT, request.getUri());
    if (file.exists()) {
      fis  = new FileInputStream(file);
      int ch = fis.read(bytes, 0, BUFFER_SIZE);

      while (ch != -1) {
        output.write(bytes, 0, ch);
        ch = fis.read(bytes, 0, BUFFER_SIZE);
      }
    }
    else {
      // file not found
      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());
    }
  }
  catch (Exception e) {
    // thrown if cannot instantiate a File object
    System.out.println(e.toString() );
  }
  finally {
    if (fis != null)
      fis.close();
  }
}


 

教程录入:dping    责任编辑:dping 
  • 上一篇教程:

  • 下一篇教程:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关教程
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    IE教程《评论:微软IE 7是为
    Trojan-PSW.Win32.OnLineGam
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    极品网
    | 设为首页 | 加入收藏 | 友情链接 | 版权声明 |
    极品网

    Copyright 2006 Jpinw.com 极品网

    备案号:申请中

    极品网