Handling cookies
Vert.x-Web
使用CookieHandler
来支持cookies
.
你必须确保当请求需要cookies
支持的时候,你已经设置上了cookie handler
。
router.route().handler(CookieHandler.create());
Manipulating cookies
你可以向getCookie()
方法, 或者通过cookies()
方法检索出cookie
集合.
getCookie()
, 传递一个cookie name
的参数来检索出一个cookie
cookies()
, 检索出cookie
集合removeCookie
, 删除一个cookie
addCookie
, 添加一个cookie
当response headers
被写回的时候, cookies
集合会自动的被写入到response
中.
Cookies
是通过Cookie
实例进行描述的. 你可以通过该实例检索出cookie
中的name
, value
, domain
, path
或者其他的cookie
属性.
下面的例子演示了如何检索和添加cookie
router.route().handler(CookieHandler.create());
router.route("some/path/").handler(routingContext -> {
Cookie someCookie = routingContext.getCookie("mycookie");
String cookieValue = someCookie.getValue();
// Do something with cookie...
// Add a cookie - this will get written back in the response automatically
routingContext.addCookie(Cookie.cookie("othercookie", "somevalue"));
});