2

I followed this guide to read and create cookies but I can only read cookie that I create from the same subdomain.

For example if I create a cookie in http://localhost:8080/x/y/test/create I can read it from: http://localhost:8080/x/y/test/read but I cannot read it from http://localhost:8080/x/y/test2/read (Note the difference between test and test2)

Where is the problem? How could I read the cookie everywhere in my domain?

Here is the code:

CLASS 1

@Path("test")
public class Test {

    @GET
    @Path("/create")
    @Produces(MediaType.TEXT_PLAIN)
    public Response login() {
        NewCookie cookie = new NewCookie("name", "123");
        return Response.ok("OK").cookie(cookie).build();
    }

    @GET
    @Path("/read")
    @Produces(MediaType.TEXT_PLAIN)
    public Response foo(@CookieParam("name") String value) {
        System.out.println(value);
        if (value == null) {
            return Response.serverError().entity("ERROR").build();
        } else {
            return Response.ok(value).build();
        }
    }

}

CLASS 2

@Path("test2")
public class Test2 {

    @GET
    @Path("/read")
    @Produces(MediaType.TEXT_PLAIN)
    public Response foo(@CookieParam("name") String value) {
        System.out.println(value);
        if (value == null) {
            return Response.serverError().entity("ERROR").build();
        } else {
            return Response.ok(value).build();
        }
    }
}

EDIT

The problem was at creation time. Now I create the cookie in this way:

NewCookie cookie = new NewCookie("name", "123", "/", "", "comment", 100, false);
Community
  • 1
  • 1
Timmy
  • 693
  • 2
  • 8
  • 26

1 Answers1

4

It's a default behavior. To set cookie to your domain, use another constructor for cookie, and set empty domain and root path:

domain = ""
path = "/"
Artem Novikov
  • 4,087
  • 1
  • 27
  • 33