zoukankan      html  css  js  c++  java
  • [try it] Apache HTTP Server

    比较有名的三个web server,Windows下是IIS,而Linux/Unix下则主要为Appache HTTP Server (httpd), 另外还有一个专门为运行java code的Tomcat Server,当然是跟着java一起跨平台了。

    其实Appache HTTP Server也是跨平台的,比如我这两天用的就是Windows下的版本。

    基本概念
    Apache HTTP Server,或者Apache Server,或者httpd,是一个支持Unix和Linux的,提供插件机制的Web Server。 所谓Web Server,是指用来host web sites的软件,host的内容包括:database, static content与runnable code。

    假设你做了一个网站,无论是几个简单的静态网页,还是一个复杂的包含数据库,cgi的动态网站,都需要有一个web server来托管,实现browser - server间的数据传输。

     注:这里有个关于动态网页,静态网页非常不错的介绍

    下载安装

    1. Apache的官网提供了Win32版本的msi
    2. 下载安装完后,可以看到大概目录结构:
      bin - httpd等主要可执行文件
      cgi-bin - cgi脚本,程序
      conf - 配置文件,httpd.conf, mime.types... 下面这些设置都是由配置文件中的配置的决定的。
      htdocs - 静态html, javascript的文件的主目录
      include - 用来编写module的sdk
      lib - 用来编写module的sdk
      logs - 执行log,发生错误时可以参考
      manual - 文档
      modules - httpd的各个功能模块,以so为后缀名,在windows下他们其实是dll
    3. 在浏览器中输入:http://localhost/,可以看到页面显示:It Works! 这其实调用了htdocs目录下的index.html文件。
    4. 在浏览器中输入:http://localhost/cgi-bin/printenv.pl, 可以看到页面中打印出所有相关的环境变量值,这其实是执行了cgi-bin/printenv.pl文件

    操作实践

    要完成这么一个工作,通过web页面显示与添加一个sqlite数据库中的数据,这就不用手工去打开数据库并select或者insert了。 

    这些功能可以用SSI与CGI实现,这里有两篇关于SSI与CGI的不错的介绍:

    基本上,你需要在httpd.conf做如下配置来启用这些功能:

    # SSI
    Options +Includes

    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml

    # CGI
    ScriptAlias /cgi-bin/ "D:/System/Apache Software Foundation/Apache2.2/cgi-bin/"

    然后,创建一个main.shtml文件,用来设计网页的总体框架,在需要从数据库读取数据的地方,使用ssi:

    <table border="1">
    <tbody>
    <tr>
    <th valign="top" width="174">Date</th>
    <th valign="top" width="114">Goodjob</th>
    <th valign="top" width="241">Explain</th>
    </tr>
    <!--#exec cgi="/cgi-bin/webpm/yexiaodata.pl"-->
    </tbody></table>

    在cgi脚本中,调用sqlite3打开数据库,查询数据并格式化后print出来,该脚本会在每次页面加载或者刷新时被调用。

    需要注意的是:cgi脚本的第一行,必须是打印content type:

    print "Content-type: text/plain;
    or
    print "Content-type: text/html;

    对于cgi,第二个需要注意的是你要在第一行指定该脚本的解析器,如:

    #!"C:/Program Files/perl64/bin/perl.exe" 

    当然,你也可以用c语言直接编写可行性文件作为cgi。
    perl与c都有一些成熟的cgi库:

    然后,插入数据,则使用html form,设计好需要的数据输入,并把form的action设置为某个cgi脚本:

    <form action="/cgi-bin/webpm/updateyexiao.pl" method=GET>
    <fieldset>
    <legend>New: </legend>
    Date:<br><input name="date" size="30" value="2012-01"> <br>
    Goodjob:<br><select name="goodjob"><option selected>1</option><option>0</option></select><br>
    Explain:<br><textarea name="explain">NULL</textarea><br>
    <input value="Submit" type="submit">
    </fieldset>
    </form>

    这样,当submit按钮被按下时,form中的数据通过url传给了cgi脚本,cgi脚本中可以通过QUERY_STRING环境变量拿到:

    REQUEST_METHOD: GET
    QUERY_STRING: date=2012-01&goodjob=1&explain=NULL


    于是,我们可以在cgi脚本中解析用户输入的数据,并组成sql语句插入数据库。

    一般这么解析:

    # Get the name and value for each form input:
    my @pairs = split(/&/, $ENV{"QUERY_STRING"});

    my %Form;
    # Then for each name/value pair....
    foreach my $pair (@pairs) {
    # Separate the name and value:
    my ($name, $value) = split(/=/, $pair);
    # Convert + signs to spaces:
    $value =~ tr/+/ /;
    # Convert hex pairs (%HH) to ASCII characters:
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    # Store values in a hash called %FORM:
    $Form{$name} = $value;
    }



  • 相关阅读:
    s3c2440 nand flash 拷贝实验
    DE270数字系统设计(4)基于Nios的LCD显示
    DE270数字系统设计实验(3)移位寄存器
    s3c2440启动时的内存拷贝过程分析
    s3c2440基本io/ports led点亮
    (转)X11/Xlib.h:没有该文件或目录
    NAND Flash读写技术
    Http中ContentType的取值讲解
    Json对象和Json字符串的区别
    table() function
  • 原文地址:https://www.cnblogs.com/baiyanhuang/p/2316539.html
Copyright © 2011-2022 走看看