package main
import (
"crypto/md5"
"fmt"
"html/template"
"io"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
//登录方法
func login(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //将表单提交的数据格式化,很重要
//得到页面
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("test.html")
t.Execute(w, token) //token作为参数,传递到前台的Name为token的对象
} else {
//post方法,提交表单数据
fmt.Println("参数:", r.Form)
name := strings.Join(r.Form["username"], "")
psd := strings.Join(r.Form["password"], "")
//通过Token,验证唯一性
token := r.Form.Get("token")
if token != "" {
if name == "AAA" && psd == "123456" {
fmt.Println("1111")
}
//后台验证表单输入
if len(name) > 4 { //len()==0//判断空值,必填项验证常使用
fmt.Println("长度超过4")
}
//判断数字
getint, err := strconv.Atoi(psd)
if err != nil {
fmt.Println("转化为数字失败!")
}
if getint > 123456 {
fmt.Println("数字不正确!")
}
//使用正则表达式
if m, _ := regexp.MatchString("^[0-9]+$", psd); !m {
fmt.Println("不是数字!")
}
//下拉列表的验证
slice := []string{"apple", "pear", "banane"}
for _, v := range slice {
if v == r.Form.Get("fruit") {
fmt.Println(v)
}
}
//单选按钮
sex := r.Form.Get("gender")
if sex == "1" {
fmt.Println("男")
} else if sex == "2" {
fmt.Println("女")
}
//复选框
a := r.FormValue("interest") //默认得到第一个值,即r.FormValue("interest")==r.Form["interest"][0]
fmt.Println(a)
b := strings.Join(r.Form["interest"], ",")
fmt.Println(b)
c := r.Form["interest"][0] //得到第一个值
fmt.Println(c)
//时间的处理
d := r.FormValue("datetime")
fmt.Println("时间1:", d)
shijian := strings.Split(d, "-")
fmt.Println("时间2:", shijian)
fmt.Println("年:", shijian[0], ",月:", shijian[1], ",日:", shijian[2])
/***********************
#string到int
int,err:=strconv.Atoi(string)
#string到int64
int64, err := strconv.ParseInt(string, 10, 64)
#int到string
string:=strconv.Itoa(int)
#int64到string
string:=strconv.FormatInt(int64,10)
***********************/
nian := shijian[0]
yue := shijian[1]
tian := shijian[2]
year1, err := strconv.Atoi(nian)
if err != nil {
fmt.Println("获取年失败!")
} else {
fmt.Println("年:", year1)
}
month1, err := strconv.Atoi(yue)
if err != nil {
fmt.Println("获取月失败!")
} else {
fmt.Println("月:", month1)
}
day1, err := strconv.Atoi(tian)
if err != nil {
fmt.Println("获取天失败!")
} else {
fmt.Println("日:", day1)
}
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
fmt.Println("时间格式3:", t)
//测试函数,得到月份的英文
yuefen := getMonth(month1)
fmt.Println(month1, "月的英文是:", yuefen)
//身份证验证
if m, _ := regexp.MatchString(`^([1-8])(\d{16})([0-9]|X)$`, r.Form.Get("usercard")); !m {
fmt.Println(r.Form.Get("usercard"), "错误!")
} else {
fmt.Println(r.Form.Get("usercard"), "正确!")
}
//转义,预防跨站脚本
fmt.Println("menu1:", template.HTMLEscapeString(r.Form.Get("menu"))) //输出到服务器端
fmt.Println("menu2:", template.HTMLEscapeString(r.Form.Get("menu")))
template.HTMLEscape(w, []byte(r.Form.Get("menu"))) //输出到客户端
}
}
}
//跳转到此页面
func gotoHome(w http.ResponseWriter, r *http.Request) {
//得到页面
if r.Method == "GET" {
t, _ := template.ParseFiles("goto.html")
t.Execute(w, nil)
} else {
testval := r.FormValue("val")
fmt.Println(testval)
}
}
//主函数
func main() {
http.HandleFunc("/", gotoHome) //设置访问的路由
http.HandleFunc("/login", login) //设置访问的路由
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
} else {
log.Fatal("登录成功!")
}
}
//根据值,得到月份
func getMonth(no int) string {
var yuefen string
switch no {
case 1:
yuefen = "January"
break
case 2:
yuefen = "February"
break
case 3:
yuefen = "March"
break
case 4:
yuefen = "April"
break
case 5:
yuefen = "May"
break
case 6:
yuefen = "June"
break
case 7:
yuefen = "July"
break
case 8:
yuefen = "August"
break
case 9:
yuefen = "September"
break
case 10:
yuefen = "October"
break
case 11:
yuefen = "November"
break
case 12:
yuefen = "December"
break
default:
/*****
获取当前时间的年月日
year:=time.Now().Year()
month:=time.Now().Month()//time.Now().Month().String()
day:=time.Now().Day()
**/
yuefen = time.Now().Month().String()
break
}
return yuefen
}
2-》前台页面(test.html和goto.html)
test.html页面主要做的是表单的验证,获取值等操作
<!Doctype html>
<html>
<head>
<title>测试调用后台程序</title>
</head>
<body>
<form action="http://127.0.0.1:9090/login" method="post">
<label>用户名:</label><input type="text" placeholder="请输入用户名" name="username"/>
<label>密 码:</label><input type="password" name="password"/>
<label>下拉表:</label>
<select name="fruit">
<option value="apple">apple</option>
<option value="pear">pear</option>
<option value="banane">banane</option>
</select>
<label>单选:</label>
<input type="radio" name="gender" value="1" checked="checked">男
<input type="radio" name="gender" value="2">女
<label>复选框:</label>
<input type="checkbox" name="interest" value="football">足球
<input type="checkbox" name="interest" value="basketball">篮球
<input type="checkbox" name="interest" value="tennis">网球
<label>时间:</label>
<input type="text" name="datetime"/>
<br/>
<label>身份证:</label>
<input type="text" name="usercard"/>
<label>测试转义:</label>
<textarea name="menu" cols="50" rows="10"></textarea>
<input type="hidden" name="token" value="{{.}}">
<button type="sumbit" value="登录" id="login">登录</button>
</form>
</body>
</html>
Goto.html页面主要做为刚开始测试页面
<!DocType html>
<html>
<head>
<title>跳转页面</title>
</head>
<body>
<form action="http://127.0.0.1:9090/" method="post">
<input name="val" placeholder="请输入值:"/>
<button type="sumbit" value="返回">返回</button>
</form>
</body>
</html>