zoukankan      html  css  js  c++  java
  • 简单的cookie实例

    首先新建一个简单的login.htm前端页面,利用nvelocity引擎模板。

    <body>
    <form action="Login.ashx" method ="post">
    
        <table>
            <tr><td>用户名:</td><td><input type ="text" name="UserName" value="$Model.UserName"</td></tr>
            <tr><td>密码</td><td><input type="password" name="Password" value="$Model.Password"/></td></tr>
            <tr><td></td><td><input type="submit" name="Login" value="登录" /></td></tr>
        </table>
    </form>
    
    </body>
    public class CommonHelper
        {
            /// <summary>
            /// 用data填充模板渲染生成html返回
            /// </summary>
            /// <param name="templateName"></param>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string RenderHtml(string templateName, object data)
            {
    
                //模版引擎代码
                VelocityEngine vltEngine = new VelocityEngine();//实例化一个velocity模版引擎
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
                vltEngine.Init();
    
                VelocityContext vltContext = new VelocityContext();
                vltContext.Put("Model",data);//设置参数,在模板中可以通过$data来引用
                //把person对象作为传入p
    
                Template vltTemplate = vltEngine.GetTemplate(templateName);
                System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                vltTemplate.Merge(vltContext, vltWriter);
    
                string html = vltWriter.GetStringBuilder().ToString();//得到html这个模板
                return html;
            }
        }


    再建立一个login.ashx的一般处理程序。

      public class Login : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
               string login=context.Request ["Login"];
            //取出这个对象,并且盘算是否为空
    if (string .IsNullOrEmpty (login )) {           //取出cookie中的值
    HttpCookie cookie2
    =context.Request .Cookies["Password"]; HttpCookie cookie=context.Request .Cookies ["UserName"]; string username; if (cookie ==null ) { username = ""; } else { username = cookie.Value; } string password; if (cookie2==null ) { password = ""; } else { password = cookie2.Value; } var data = new { UserName=username,Password=password }; context.Response.Write(CommonHelper .RenderHtml ("Login.htm",data)); } else { string username=context .Request ["UserName"];           //新建cookie
    HttpCookie cookie
    = new HttpCookie("UserName",username);            //设定过期的时限
    cookie.Expires
    = DateTime.Now.AddDays(7); context.Response.SetCookie(cookie); //密码的 string password = context.Request["Password"]; //新建一个cookie实例 HttpCookie cookie2 = new HttpCookie("Password", password); //设定周期 cookie2.Expires = DateTime.Now.AddDays(5); //创建响应cookie context.Response.SetCookie(cookie2); context.Response.Write("登陆成功"); } } public bool IsReusable { get { return false; } } } }
  • 相关阅读:
    Swift的函数与函数指针、闭包Closure等相关内容介绍
    spring+dubbo整合
    常用设计模式-适配器模式
    常用设计模式-工厂模式
    java动态代理
    程序对关系型数据库批量操作
    springboot整合mybatis
    JAVA代码实现多级树结构封装对象
    springboot集成redis缓存
    springboot入门
  • 原文地址:https://www.cnblogs.com/dandandeyoushangnan/p/4548662.html
Copyright © 2011-2022 走看看