实验3
- 格式:doc
- 大小:227.50 KB
- 文档页数:4
山东理工大学实验报告纸姓名:卢鑫学院:计算机科学与技术学院班级:数媒1001班成绩:
课程名称:互联网与网站建设教师签字:_________ 实验项目:实验3:内置对象的使用指导老师:
实验目的:掌握Response对象、Request对象的用法。
掌握Session对象的用法。
掌握Cookie对象的用法。
实验仪器:(材
料,工具)
实验内容:
1.打开网站“运用ResPonse对象”,阅读程序;然后创建一个 网站,运用Response和Request对象实现用户登录。
登录界面:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["CheckNum"] = 0;
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (this.txtAdminName.Text.Trim() == "" || this.txtAdminPwd.Text.Trim() == "")
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名和密码不能为空')</script>");
return;
}
if (txtAdminName.Text == "admin" && txtAdminPwd.Text == "admin")
{
Response.Redirect("Default3.aspx?userName=" + txtAdminName.Text + "");
}
else
{
Session["CheckNum"] = Int32.Parse(Session["CheckNum"].ToString()) + 1;
int i = Int32.Parse(Session["CheckNum"].ToString());
if (i > 1)
{
Response.Redirect("Default2.aspx");
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('你输入的用户和密码不匹配请重新输入')</script>");
}
}
}
成功登录:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["userName"] != null)
{
Label1.Text = " 欢迎你:" + Request.QueryString["userName"] + "<br/>";
Label1.Text += " 当前时间是:<br/> "+ DateTime.Now.ToString();
}
else
{
Response.Redirect("Default.aspx");
}
}
}
多次输入错误:
<form id="form1"runat="server">
<div>
<center>
你登录次数过于频繁,已经尝试三次,并且用户密码不匹配,<br/>
如果忘记密码请确认你的密码正确,再进行登录
<br/>
<span lang="zh-cn">如果还没有用户,请首先申请账户,再登录</span>
</center>
</div>
</form>
2打开网站“Demo-Session”,阅读程序;然后创建一个网站,运用Session对象实现购物信息的临时保存。
.Session["my"] = "它是一个宝贝!";
if (Session["my"] != null)
{
Response.Write("购物信息临时保存成功!");
}
else
{
Response.Write("购物信息临时保存不成功!");
}
3创建一个网站“Demo-Cookies”,运用Cookie对象实现在页面载入时检索某Cookie是否存在以进行页面的初始化。
.Response.Cookies["mycookie"].Value = "my";
Response.Cookies["mycookie"].Expires = DateTime.Now.AddDays(1);
if (Request.Cookies["mycookie"] == null)
{ Response.Write("cookie不存在!"); }
else
{ Response.Write("cookie存在!"); }
实验结论:
教师评语:。