goahead(嵌入式Web服务器)之asp、goform篇

GoAhead简介
GoAhead WebServer,它是一个源码,免费、功能强大、可以在多个平台运行的嵌入式WebServer。
GoAhead提供了多种方法编写动态页面,包括asp过程、GoForms过程和embedded JavaScript。GoAhead主要利用asp过程动态获取系统信息然后显示在页面上,GoForms过程则主要用来处理用户指令,例如控制设备和修改配置等.

GoForms过程
GoForm实现为一个URL处理器,它会解释以"/goform"或者"/action"等开始的URL,最后通过websFormDefine函数调用进行关联

1、构建web网页
网页命名为login.html,内容如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登陆验证</title>
</head>
<body>
 <form action=/action/login method="post">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="right">用户名</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td align="right">密 码</td>
<td><input type="password" name="Password"></td>
</tr>
<tr>
<td><input type="submit" value="登 录"></td>
<td><input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>

2、注册表单
websDefineAction(“login”, login_fun);
注:action=/action/login的“login”为websFormDefine函数调用的标志,通过识别标志来调用注册的login_fun函数的实现。

3、关联函数

static void login_fun(Webs *wp)
{
	// get the input value in query stream
	char *username =NULL;
	char *password =NULL;

	username = websGetVar(wp, "Username", NULL);
	printf("username = %s\n",username);
	password = websGetVar(wp, "Password", NULL);
	printf("password = %s\n",password);
	
	websHeader(wp);
	websWrite(wp, "Name: %s, Address %s", username, password);
    websFooter(wp);
    websDone(wp);
}

4、将login.html文件考到目标文件goahead的相同目录下、网页文件的识别路径可以自主设置、路径设置请参考上一篇文章。

5、编译、更新libgo.so 重新运行goahead

6、在网页上登录:http://设备IP/login.html
如图显示:
在这里插入图片描述
点击登录按键后,将在网页显示:在这里插入图片描述
**表单goform的另一个形式:**通过url可以直接与goahead服务器的通信
如输入 http://10.82.16.61/action/login?Password=1&Username=chen
在这里插入图片描述

**总结:**通过表单过程,可将数据通过网页传到goahead服务器,再由goahead服务器处理对应数据,从而实现了网页远程控制或者调试设备的功能

asp过程:
用来生成显示在页面中的动态数据

1、创建login.asp文件,内容如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登陆验证</title>
</head>
<body>
 <form action=/action/login method="post">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="right">用户名</td>
<td><input type="text" name="Username" value="<%AspTest("Username");%>"></td>
</tr>
<tr>
<td align="right">密 码</td>
<td><input type="text" name="Password" value="<%AspTest("Password");%>"></td>
</tr>
<tr>
<td><input type="submit" value="登 录"></td>
<td><input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>

2、注册创建函数
websDefineJst(T(“AspTest”), AspTest);

注:asp页面中出现<%AspTest(“Username”);%>,webserver 会调用AspTest函数来输出页面,Username为AspTest函数提交的变量标志。

3、asp函数定义

char      g_test_username[32] = {0};
char      g_test_password[32] = {0};
/* 与ASP过程绑定的C函数     */
/* 与ASP过程绑定的C函数 	*/
static int AspTest(int eid, Webs * wp, int argc, char **argv)
{
	char  *name;
	char buffer[128];
	/* 判断参数是否过少 */
	printf("argc = %d\n",argc);
	if (jsArgs(argc, argv, T("%s"), &name) < 1) {
		websError(wp, 400, T("Insufficient args\n"));
		return -1;
	}
	/* 根据页面上input标签内的name属性判断将什么变量显示到页面上对应的文本框内 */
	if (!strcmp(name,T("Username")))
	{	
		sprintf(buffer, "%s", g_test_username);
		return websWrite(wp, T("%s"),buffer);
		//websWrite()是goahead的API,可以将内容写回html页面
	}
	else if (!strcmp(name,T("Password")))
	{
		sprintf(buffer, "%s", g_test_password);	 
		return websWrite(wp, T("%s"),buffer);
	}
	else
	{
		return -1;
	}
}

4、将login.asp文件到目标文件goahead的相同目录下、网页文件的识别路径可以自主设置、路径设置请参考上一篇文章。

5、规则route设置:
在route.txt里,需要将route uri=/ extensions=jst handler=jst改为route uri=/ extensions=jst|asp handler=jst,这样才能注册asp功能,如果设置成这样,asp调用失败。

6、测试例子:
打开两个网页,一个网页用来设置值,一个网页刷新去获取之前设置的值。

总结:总体而言,表单form可以实现了网页远程控制或者调试设备的功能,而asp可以动态获取并显示在网页上,可用于远程实时监控数据方面。

goahead系统学习章节:
goahead(嵌入式Web服务器)之文件传输篇
goahead(嵌入式Web服务器)之交叉编译、移植篇
goahead(嵌入式Web服务器)之cgi篇
goahead(嵌入式Web服务器)之调试篇
goahead(嵌入式Web服务器)之openssl证书制作篇
goahead(嵌入式Web服务器)之openssl 应用篇
goahead(嵌入式Web服务器)之总结篇

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值