以下是在Restlet服务器资源中获取提交的HTML表单值的正确方法(使用内容类型
application/x-www-form-urlencoded
)。这实际上就是你所做的。
public class MyServerResource extends ServerResource {
@Post
public Representation handleForm(Representation entity) {
Form form = new Form(entity);
String user = form.getFirstValue("user");
String password = form.getFirstValue("password");
(...)
}
}
在您的情况下,HTML表单实际上未发送,因为您没有为表单定义任何属性
name
。我使用了您的HTML代码,但发送的数据为空。您可以使用Chrome开发者工具(Chrome)或Firebug(Firefox)进行检查。
POST /myrestlet HTTP/1.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection: keep-alive
Host: localhost:8182
Referer: http://localhost:8182/static/test.html
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
您应该为您的HTML表单使用类似于以下内容的代码:
<form action="/test" method="post">
<input type="text" name="val1" size="50" value="5"/>
<input type="text" name="val2" size="50" value="C:\Temp"/>
(and a few other input type texts)
<input type="submit" value="send">
</form>
在这种情况下,请求将是:
POST /myrestlet HTTP/1.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection: keep-alive
Host: localhost:8182
Referer: http://localhost:8182/static/test.html
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Content-Length: 23
Content-Type: application/x-www-form-urlencoded
val1=5&val2=C%3A%5CTemp
希望这能对你有所帮助,Thierry。