Package Main
Import (
"FMT"
"Html/template"
"Log"
"Net/http"
"Strings"
)
Func Main () {
http. Handlefunc ("/", SayHello) //Set Access routing
http. Handlefunc ("/login", login)//Set Access routing
ERR: = http. Listenandserve (": 9999", nil)//Set the listening address and port
If err! = Nil {
Log. Fatal ("Listenandserve:", err)
}
}
Func SayHello (w http. Responsewriter, R *http. Request) {//For Output first page
R.parseform ()
Fmt. Println (R.form)
Fmt. PRINTLN ("Path", R.url. Path)
Fmt. PRINTLN ("scheme", R.url. Scheme)
For k, V: = Range R.form {
Fmt. Println ("Key:", K)
Fmt. Println ("Value:", strings. Join (V, ""))
}
Fmt. fprintf (W, "Hello Golang")
}
/*
The function performs different actions depending on the submission method.
If the commit method is get, output the login page to the client
Gets the form content submitted by the client if the submission method is post
*/
Func Login (w http. Responsewriter, R *http. Request) {
Fmt. Println ("Method:", R.method)
if R.method = = "GET" {
T, _: = template. Parsefiles ("login.html")
T.execute (W, Nil)
} else {
R.parseform ()//The Parseform method call here is required, otherwise the form contents cannot be obtained below
The following code is a logical processing of the contents of a form
Fmt. Println ("Username:", r.form["username"])
Fmt. Println ("Password:", r.form["password"])
}
}
The console runs directly:
Access the first page using IE:
Console output:
Next, use IE to access the login page:
Console output:
Enter the user name and password, click the login button, submit the data to the background for logical processing:
Console output:
The user name and password in the form are printed in the background.