【发布时间】:2011-11-06 01:35:03
【问题描述】:
我在 myeclipse 中使用 jersey jax-rs 作为我的项目的后端,并使用 jsp 作为前端。我想在成功登录后从服务器设置 cookie。在球衣的官方文档中,我只能找到如何通过球衣获取cookie。有没有人可以给我一个演示来做这样的事情?
这是我的登录部分,我返回响应并重定向到 URL“/”,这意味着 index.jsp。
@Path("/login")
@POST
@Consumes("application/x-www-form-urlencoded")
public Response login(@FormParam("email") String email,
@FormParam("password") String password) {
Map<String, Object> model = MapFactory.newHashMapInstance();
model.put("email", email);
model.put("password", password);
loginCheck(model);
if (model.get("emailCheck").equals("ok")
&& model.get("passwordCheck").equals("ok")) {
return Response.ok(
new Viewable("/index", new NewCookie("name",
"Hello, world!"))).build();
} else {
return Response.ok(new Viewable("/login", model)).build();
}
}
这是我的“/”部分:
@GET
@Produces("text/html")
public Response getIndex(@CookieParam("name") String name) {
HashMap<String, Object> model = MapFactory.newHashMapInstance();
model.put("name", name);
System.out.println("cookie name:\t" + name);
return Response.ok(new Viewable("/index", model)).build();
}
每次运行这段代码,我发现我无法从索引部分获取cookie。如果你也被这个问题困扰并最终解决了,请给我一些指导,谢谢。
【问题讨论】: