Web的初步篇:前台(HTML)和后台(一般处理程序)

Web的初始篇:前台(HTML)和后台(一般处理程序)

在我们浏览的一个网页中,一般分为两部分,前台和后台。

前台是我们可以看到的,而后台却是希望我们看不到的。前台把网页装扮的很精美,而后台就是来处理这些复杂的信息。前台的代码是下载到本机上可以看到,而后台是放在服务器上的,不会被看到的。

首先给大家一个简单的前台后台的模子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form action="Handler1.ashx">
<div>
<p>用户名:<input type="text" name="Name" /><input type="submit" value="提交" /></p>
 </div></form>
</body>
</html>

Web的初步篇:前台(HTML)和后台(一般处理程序)

上面的是HTML的代码也就是前台和效果。

下面的就是后台了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace _9._17._3
{
    /// <summary>
    /// Handler2 的摘要说明
    /// </summary>
    public class Handler2 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";//原来括号内是text/plain
            string a = context.Server.MapPath("HTMLPage2.htm");//获取路径、
            context.Response.WriteFile(a);//这一句相当于下面注释掉的两句,代码要简练嘛
            //string d = File.ReadAllText(a); //读取路径a中的内容给了string类型的变量d
            //context.Response.Write(d);   //输出string变量d中的内容
            string g = context.Request["Name"];   //获取网页中的内容,name="Name"的value
            string er = context.Request["ad"];
            if (!string.IsNullOrEmpty(g))  //string.IsNullOrEmpty()是判断括号内的内容是不是为空
                context.Response.Write("有东西!");
            else
                context.Response.Write("没有东西,是NULL!");
            string gh = "asdf";
            gh.Replace("asdf","sfad");//这是替换字符串中指定的内容
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


有注释,自己看就能看懂了,这就是一个模子,和C#有相通之处。

下面用后台写一个每点击一下就加一的网页:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form action="Handler3.ashx" method="post">
<input type="text" value="0" name="uy"/><input type="submit" value="加"/>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace _9._17._3
{
    /// <summary>
    /// Handler3 的摘要说明
    /// </summary>
    public class Handler3 : IHttpHandler
    {
        int a;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            //context.Response.Write("Hello World");
            a = int.Parse(context.Request["uy"]); a = a + 1;
            context.Response.Write(@"<form action='Handler3.ashx' ><input type='text' value='"+a+"' name='uy'/><input type='submit' value='加'/></form>");
            a += 1;

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


效果图:

 

Web的初步篇:前台(HTML)和后台(一般处理程序)

每当你点击“加”的时候就会加一,并在左边的textbox里显示。