Go web表单
2024-09-20 14:20:05
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func sayhelloName(w http.ResponseWriter,r *http.Request){ //访问主体回应
r.ParseForm() //解析url传递的参数,对于POST则解析响应包的主体
//如果没有调用ParseForm方法则无法获取表单的数据.
fmt.Println(r.Form)//输出url的参数,到服务器上
fmt.Println(r.URL.Path)//输出url的路径
fmt.Println(r.URL.Scheme)//输出方案
for k,v :=range r.Form{
fmt.Println(k)
fmt.Println(strings.Join(v,""))//加入字符串输出看效果
}
fmt.Fprintf(w,"hello world")//回应内容
}
func login(w http.ResponseWriter,r *http.Request){
fmt.Println(r.Method)//输出客户端请求的方法
if r.Method=="GET"{
t,_:=template.ParseFiles("login.html") //解析html代码,就是那个表单
log.Println(t.Execute(w,nil))
}else{
r.ParseForm()
fmt.Println(r.Form["username"]) //字典获取username的值
fmt.Println(r.Form["password"])
}
} func main(){
http.HandleFunc("/",sayhelloName) //设置访问的路由
http.HandleFunc("/login",login)
err:=http.ListenAndServe(":9999",nil)//设置监听端口
if err !=nil{
log.Fatal("listenandserver",err)//报错则输出错误并退出
}
}
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
最新文章
- Windows 8(虚拟机环境)安装.NET Framework3.5(includes .NET 2.0 and 3.0)
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- UIScrollView实现图片轮播器及其无限循环效果
- Android基础之项目结构分析
- HTML5 Shiv – 让该死的IE系列支持HTML5吧
- Oracle监听器—动态注册
- 摘录android工具类
- .Net XML操作 <;第二篇>;
- C#传递参数大集合
- Python版C语言词法分析器
- c++内存优化:二级间接索引模式内存池
- ChatterBot之linux下安装mongodb 02
- P1045 麦森数
- HTML多表头表格
- PAT1004:Counting Leaves
- linux下设置phantomjs环境变量
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
- 写一篇博文介绍JSP
- Python语言——Python语言概述
- 跨域学习笔记3--web.config设置之system.webServer 详细介绍,为网站设置默认文档