首 页 教育新闻课件中心论文中心教学教案试题中心语文专题综合下载技术教程公务员  
设为首页
加入收藏
联系我们
您当前的位置:中国教育资源网 -> 技术教程 -> 网络相关 -> 服务器 -> Windows服务器 -> 技术内容 退出登录 用户管理

基于Jave的Web服务工作机制(6)Windows服务器教程

论文作者:佚名  论文来源:不详  论文发布时间:2006-6-19 18:10:52  论文发布人:chjchjchj

减小字体 增大字体

              摘要:暂无  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();
  }
}

[] [返回上一页] [打 印] [收 藏]  
 ∷相关技术评论  (评论内容只代表网友观点,与本站立场无关!) [查看发表评论...]
 
 中国教育资源网免费技术教程下载中心-站内广告 站内广告 中国教育资源网免费技术教程下载中心-站内广告 
 中国教育资源网站内搜索 站内搜索 中国教育资源网站内搜索 
 

   
 中国教育资源网免费技术教程下载中心-栏目导航 栏目导航 中国教育资源网免费技术教程下载中心-栏目导航 
· Windows 9XMEXP · Windows NT20002003
· LinuxBSD · 系统综合
· IISApache · 硬件技术
· Web服务器 · FTP服务器
· 邮件服务器 · 域名服务器
· Windows服务器 · 代理服务器
· 服务器综合
 
中国教育资源网免费技术教程下载中心-相关教程  相关技术 中国教育资源网免费技术教程下载中心-相关教程
· 基于Java的企业分布
· 基于Java的IDEA加密
· 基于Java的建造设计
· [图文] 基于Java的解释器设
· 开发基于Java的图形
· [图文] 基于Java的代理设计
· 基于Jave的Web服务工
· 基于Jave的Web服务工
· 基于Jave的Web服务工
· 基于Jave的Web服务工
 中国教育资源网免费技术教程下载中心-本月热门教程 本月热门 中国教育资源网免费技术教程下载中心-本月热门教程 
 
 中国教育资源网免费技术教程下载中心-本日热门论文 本日热门 中国教育资源网免费技术教程下载中心-本日热门论文 
 
关于本站 - 网站帮助 - 免费课件 - 美容 - 绿色软件 - 软件下载 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 网站留言
浙ICP备06010405号 Email:cnkjz@163.com 技术支持:名流设计
版权所有 Copyright© 2002-2004 名流