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>

  

最新文章

  1. Windows 8(虚拟机环境)安装.NET Framework3.5(includes .NET 2.0 and 3.0)
  2. struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
  3. UIScrollView实现图片轮播器及其无限循环效果
  4. Android基础之项目结构分析
  5. HTML5 Shiv – 让该死的IE系列支持HTML5吧
  6. Oracle监听器—动态注册
  7. 摘录android工具类
  8. .Net XML操作 &lt;第二篇&gt;
  9. C#传递参数大集合
  10. Python版C语言词法分析器
  11. c++内存优化:二级间接索引模式内存池
  12. ChatterBot之linux下安装mongodb 02
  13. P1045 麦森数
  14. HTML多表头表格
  15. PAT1004:Counting Leaves
  16. linux下设置phantomjs环境变量
  17. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
  18. 写一篇博文介绍JSP
  19. Python语言——Python语言概述
  20. 跨域学习笔记3--web.config设置之system.webServer 详细介绍,为网站设置默认文档

热门文章

  1. check_mk通用应用检测插件
  2. python编写的简单的mysql巡检脚本
  3. HTML:一个form表单有两个按钮,分别提交到不同的页面
  4. Template Pattern &amp; Strategy Pattern
  5. python入门24 json模块
  6. Coreseek:常见问题2
  7. Django:ORM关系字段
  8. Python:IPC-Pipe与IPC-Manger
  9. Bokeh 学习
  10. java实现按拼音排序