go语言实现一个http服务器完成静态页面的账号和密码登陆
1、静态页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登陆页面</title>
<link rel="stylesheet" type="text/css" href="/css/main.css" />
</head>
<body>
<h1>登陆页面</h1>
<form method="post" action="/Login">
<table>
<tr>
<td>用户名</td><td><input type="text" name="admin"></input></td>
</tr>
<tr>
<td>密码</td><td><input type="password" name="password"></input></td>
</tr>
<tr>
<td><input type="submit" value="登陆" style="width:100px;height:25px"></input></td>
</tr>
</table>
</form>
</body>
</html>
2、go语言实现HTTP服务器
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
func TestHandle(res http.ResponseWriter, req *http.Request) {
admin := req.FormValue("admin")
password := req.FormValue("password")
fmt.Println(admin)
fmt.Println(password)
if admin != "zhanghao" || password != "123456" {
res.Write([]byte("Login Fail,Please Try Again!"))
} else {
res.Write([]byte("test for web : " + admin + " " + password))
}
}
func main() {
port := "80"
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
t, err := template.ParseFiles("login.html")
if err != nil {
log.Println("err")
}
t.Execute(res, nil)
})
http.HandleFunc("/Login", TestHandle)
fmt.Println("start http server at:", port)
http.ListenAndServe(":"+port, nil)
}
浏览器登陆成功界面