Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cookie in Jersey?

I am using jersey jax-rs in myeclipse as backend of my project and jsp as frontend. I want to set cookie from server after successful login. In the jersey's official document, I can only find how to get cookie by jersey. Does anyone can give me a demo to do such things?

This is my login part and I return a response and redirect to URL "/" which means 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();
    }
}

This is my "/" part:

@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();
}

Every time I run this code, I find that I cannot get cookie from the index part. If you also ever bothered by this question and finally solved it, plz give me some directions, thanks.

like image 502
mons Avatar asked Aug 29 '11 14:08

mons


People also ask

How do you set cookies in Jersey client?

put("Cookie", cookies); } ClientResponse response = getNext(). handle(request); if (response. getCookies() != null) { if (cookies == null) { cookies = new ArrayList<Object>(); } // simple addAll just for illustration (should probably check for duplicates and expired cookies) cookies.

How do I set cookies in REST API?

To set a cookie in REST API response, get the Response reference and use it's cookie() method.

How do I add cookies in ResponseEntity?

Another way is to add the cookie as raw Set-Cookie header while building ResponseEntity object: HttpHeaders headers = new HttpHeaders(); headers. add("Set-Cookie","platform=mobile; Max-Age=604800; Path=/; Secure; HttpOnly"); ResponseEntity. status(HttpStatus.

What are cookies in REST API?

A cookie is a piece of data that a server sends in the HTTP response. The client (optionally) stores the cookie and returns it on subsequent requests. This allows the client and server to share state. To set a cookie, the server includes a Set-Cookie header in the response.


1 Answers

To set the cookie in your example, you can do something like this:

return Response.ok(new Viewable("/index", model))
               .cookie(new NewCookie("name", "Hello, world!"))
               .build();

But if you want to redirect to "/" you would also need to return 3xx response instead of 200, for example:

return Response.seeOther("/")
               .cookie(new NewCookie("name", "Hello, world!"))
               .build();
like image 80
ivan.cikic Avatar answered Oct 21 '22 02:10

ivan.cikic