go语言实现一个http服务器完成静态页面的账号和密码登陆

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)
}

浏览器登陆成功界面





  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值