我学的是javaweb,最近觉得自己学的知识远远不够,所以在课余时间开始学起了c#。也想多掌握一门编程语言,对于学过java的人来说学习c#是比较简单的,asp.net也是一样。今天就来写个模仿登录的程序。这里会结合js来做开发。
首先做一个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>
<script type="text/javascript">
function check(){
var username=document.getElementById("name").value;
var password=document.getElementById("password").value;
if(username==""||password==""){
alert("用户名或密码不能为空");
return false;
}
}
</script>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server" action="servlet/check2.ashx" method="post">
用户名:<input type="text" name="name" id="name"/><br /><br />
密码:<input type="password" name="password" id="password"/><br /><br />
<input type="submit" value="登录" οnclick="javascript:return check()"/>
</form>
</body>
</html>
编写一般处理程序,如果用户名和密码都是tom的打印登录成功,否则登录失败
<%@ WebHandler Language="C#" Class="check2" %>
using System;
using System.Web;
public class check2 : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
string username = context.Request["name"];
string password = context.Request["password"];
if (password.Equals("tom") && username.Equals("tom"))
{
context.Response.Write("登录成功");
}
else
{
context.Response.Write("登录失败");
}
}
public bool IsReusable {
get {
return false;
}
}
}
学习java的人可能会这么写string username = context.Request("name").这里写成小括号就会报错
到这里程序就结束了,下面是运行效果