• Ajax技术三种实现方式之xmlhttp+httphandler篇 (三)


    01.一、             改进的方式:xmlhttp+httphandler  
    02. 
    03.应用举例  
    04. 
    05.1、首先需要生成一个httphandler的ashx  
    06. 
    07.using System;  
    08. 
    09.using System.Collections.Generic;  
    10. 
    11.using System.Linq;  
    12. 
    13.using System.Text;  
    14. 
    15.using System.Web;  
    16. 
    17.   
    18. 
    19.namespace HelloWorld  
    20. 
    21.{  
    22. 
    23.    public class NewHttpHandler:IHttpHandler  
    24. 
    25.    {  
    26. 
    27.        public bool IsReusable  
    28. 
    29.        {  
    30. 
    31.            get 
    32. 
    33.            {  
    34. 
    35.                return true;  
    36. 
    37.            }  
    38. 
    39.        }  
    40. 
    41.        public void ProcessRequest(HttpContext context)  
    42. 
    43.        {  
    44. 
    45.            StringBuilder str = new StringBuilder();  
    46. 
    47.            str.Append("服务器ajax.aspx得到了您输入的信息:" +context.Request["msg"].ToString() + "<br/>您的IP地址是:");  
    48. 
    49.            str.Append(context.Request.UserHostAddress);  
    50. 
    51.            str.Append("<br/>当前服务器的时间:");  
    52. 
    53.            str.Append(DateTime.Now.ToLocalTime());  
    54. 
    55.            context.Response.Write(str.ToString());  
    56. 
    57.            context.Response.End();  
    58. 
    59.        }  
    60. 
    61.    }  
    62. 
    63.}  
    64. 
    65. 
    66.2、调用ajax页面  
    67. 
    68.XmlHttpHttpHandler.aspx  
    69. 
    70.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XmlHttpHttpHandler.aspx.cs" Inherits="AJAX.XmlHttpHttpHandler" %>  
    71. 
    72.   
    73. 
    74.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    75. 
    76.   
    77. 
    78.<html xmlns="http://www.w3.org/1999/xhtml" >  
    79. 
    80.<head runat="server">  
    81. 
    82.    <title>XMLHTTP+HttpHandler</title>  
    83.//ajax.js查看上一篇文章  
    84.      <mce:script src="ajax.js" mce_src="ajax.js" type="text/javascript"></mce:script>  
    85. 
    86.</head>  
    87. 
    88.<body>  
    89. 
    90.    <form id="form1" runat="server">  
    91. 
    92.    <div>  
    93. 
    94.     <input id="txtMsg" type="text" value="hello,world!"/><input id="Button1" type="button"   
    95. 
    96.            value="提交给Ajax页面处理" onclick="callTheServer('HelloWorld.ashx')" /><br />  
    97. 
    98.        <br />  
    99. 
    100.        <br />  
    101. 
    102.        <br />  
    103. 
    104.        服务器返回的信息如下:  
    105. 
    106.        <div id="showmsg" style="background-color:Yellow" mce_style="background-color:Yellow">  
    107. 
    108.          
    109. 
    110.        </div>  
    111. 
    112.    </div>  
    113. 
    114.    </form>  
    115. 
    116.</body>  
    117. 
    118.</html> 
    一、             改进的方式:xmlhttp+httphandler

    应用举例

    1、首先需要生成一个httphandler的ashx

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Web;

    namespace HelloWorld

    {

        public class NewHttpHandler:IHttpHandler

        {

            public bool IsReusable

            {

                get

                {

                    return true;

                }

            }

            public void ProcessRequest(HttpContext context)

            {

                StringBuilder str = new StringBuilder();

                str.Append("服务器ajax.aspx得到了您输入的信息:" +context.Request["msg"].ToString() + "<br/>您的IP地址是:");

                str.Append(context.Request.UserHostAddress);

                str.Append("<br/>当前服务器的时间:");

                str.Append(DateTime.Now.ToLocalTime());

                context.Response.Write(str.ToString());

                context.Response.End();

            }

        }

    }


    2、调用ajax页面

    XmlHttpHttpHandler.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XmlHttpHttpHandler.aspx.cs" Inherits="AJAX.XmlHttpHttpHandler" %>

    <!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 runat="server">

        <title>XMLHTTP+HttpHandler</title>
    //ajax.js查看上一篇文章
          <mce:script src="ajax.js" mce_src="ajax.js" type="text/javascript"></mce:script>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

         <input id="txtMsg" type="text" value="hello,world!"/><input id="Button1" type="button"

                value="提交给Ajax页面处理" onclick="callTheServer('HelloWorld.ashx')" /><br />

            <br />

            <br />

            <br />

            服务器返回的信息如下:

            <div id="showmsg" style="background-color:Yellow" mce_style="background-color:Yellow">

           

            </div>

        </div>

        </form>

    </body>

    </html>
     

    ashx是什么如何创建?

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liaolian9948/archive/2010/05/12/5581329.aspx

  • 相关阅读:
    jsp+servlet实现的验证登陆
    Servlet转发
    ServletContext的使用
    Servlet获取配置信息(ServletConfig)
    Servlet线程安全问题(转载)
    编程式导航
    声明式导航
    Vue Router
    路由
    vue-cli 单文件组件 工具安装
  • 原文地址:https://www.cnblogs.com/mahaisong/p/2016956.html
Copyright © 2020-2023  润新知