Quick revision
The short answer
We read the header with @HeaderParam("X-Request-Id") and the cookie with @CookieParam("session"), build the new cookie with NewCookie.Builder (Jakarta REST 3.1 and later) setting path, a max age of 30 days, Secure, HttpOnly and SameSite LAX, and return Response.ok(entity).cookie(themeCookie).build(). Whether a theme cookie should be HttpOnly depends on whether the frontend JavaScript needs to read it.
What to remember
In this answer, we read X-Request-Id with @HeaderParam and the session cookie with @CookieParam, remembering that both can be null and that header names are case-insensitive. We built the theme cookie with NewCookie.Builder, which arrived in Jakarta REST 3.1, with path, a 30-day max age, Secure, HttpOnly and SameSite LAX, and returned it with Response.ok(entity).cookie(theme). We also saw what each attribute does, why HttpOnly only makes sense if the frontend does not read the cookie, how HttpHeaders gives all headers and cookies, and how to delete a cookie with Max-Age 0 on the same path.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We read the header with @HeaderParam("X-Request-Id") and the cookie with @CookieParam("session"), build the new cookie with NewCookie.Builder (Jakarta REST 3.1 and later) setting path, a max age of 30 days, Secure, HttpOnly and SameSite LAX, and return Response.ok(entity).cookie(themeCookie).build(). Whether a theme cookie should be HttpOnly depends on whether the frontend JavaScript needs to read it.
Why this question is asked
Headers and cookies are how a REST service learns about the request around the body: who sent it, which session it belongs to, which trace it is part of. Interviewers ask this small coding question because it checks that we know the injection annotations, the newer cookie builder, and the security attributes that every cookie decision involves. In this answer, we will recap how headers and cookies arrive, write the resource method, look at each cookie attribute and what it really does, discuss whether HttpOnly is right for a theme cookie, and see how to delete a cookie.
1A Quick Recap of Headers and Cookies
An HTTP request carries headers, name and value pairs such as X-Request-Id: 7f3a. Header names are case-insensitive, so x-request-id and X-Request-Id are the same header. A cookie is a small value that the server asked the browser to store with a Set-Cookie response header; the browser then sends it back on later requests in a single Cookie header, for example Cookie: session=abc123; theme=light.
JAX-RS gives us annotations to pull these values into method parameters. @HeaderParam injects one header, @CookieParam injects one cookie, and @Context HttpHeaders gives us all of them at once when we need to look around.
2Writing the Resource Method
Here is the full method. It reads the request id and the session cookie, and returns a response that sets the theme cookie.
Notice that both injected values may be null, because nothing forces a client to send them, so we check before relying on them. The session parameter is a jakarta.ws.rs.core.Cookie, which gives us getName() and getValue(); if we only need the value, we can declare it as String instead. NewCookie.Builder and the SameSite enum arrived in Jakarta REST 3.1 (Jakarta EE 10, Jersey 3.1, RESTEasy 6.2); on older versions we had to use the long NewCookie constructors and could not set SameSite without writing the header by hand, so check your version.
3What Each Cookie Attribute Does
Each attribute changes when the browser stores or sends the cookie, so it is worth knowing them precisely rather than copying them as a ritual.
| Attribute | Effect |
|---|---|
| Path=/ | sent with every request to this host, not only /preferences |
| Max-Age=2592000 | kept for 30 days, then the browser deletes it |
| Secure | only sent over HTTPS |
| HttpOnly | hidden from JavaScript (document.cookie cannot read it) |
| SameSite=Lax | sent on top-level navigations from other sites, not on cross-site subrequests |
Max-Age is the modern way to set lifetime; some implementations also write an Expires date for old clients, and NewCookie.Builder has an expiry(Date) method if we want to set it ourselves. Note the difference from the session cookie: a cookie without Max-Age or Expires is deleted when the browser closes.
4Should a Theme Cookie Really Be HttpOnly?
The question asks for HttpOnly, and we have set it, but a good answer explains the choice. HttpOnly protects a cookie from being read by JavaScript, which matters for secrets: if an attacker injects script into our page, they cannot steal an HttpOnly session cookie. A theme is not a secret. If our frontend reads the theme cookie to apply dark mode before the first API call, HttpOnly would break that, because document.cookie would not show it.
So the reasoning is: the session cookie must be HttpOnly, Secure and usually SameSite Lax or Strict, because it proves who the user is. The theme cookie can be HttpOnly if only the server uses it, for example to render the right stylesheet, and should not be if the browser code needs it. Secure costs nothing on an HTTPS site, so we keep it either way.
5Reading All Headers and Cookies at Once
For logging or for optional values, injecting HttpHeaders is often handier than a parameter per value.
getHeaderString joins multiple values of the same header with commas, while getRequestHeader returns them as a list, which matters for headers such as Accept that may repeat.
6Follow-up questions
How would you delete a cookie?
We send a Set-Cookie for the same name, path and domain with Max-Age 0, and usually an Expires date in the past for older clients. With the builder that is new NewCookie.Builder("theme").value("").path("/").maxAge(0).build(), returned with Response.ok().cookie(...). The path and domain must match the original exactly; a cookie set with Path=/ is not removed by a deletion sent with Path=/preferences, which is the most common reason a logout seems not to work.
What does SameSite Lax protect against?
It stops the browser from sending the cookie on cross-site subrequests such as a form POST from another site or an image or fetch to our domain, which blocks most cross-site request forgery. It still sends the cookie when the user follows a normal link to our site, so users stay logged in when they arrive from an email or a search result.
Can we read the session cookie value in a filter instead?
Yes. In a ContainerRequestFilter, requestContext.getCookies() returns the same map. That is the usual place for session lookup, because the filter can reject the request with 401 before any resource method runs, and the resource only reads the result from the security context.
7Conclusion
In this answer, we read X-Request-Id with @HeaderParam and the session cookie with @CookieParam, remembering that both can be null and that header names are case-insensitive. We built the theme cookie with NewCookie.Builder, which arrived in Jakarta REST 3.1, with path, a 30-day max age, Secure, HttpOnly and SameSite LAX, and returned it with Response.ok(entity).cookie(theme). We also saw what each attribute does, why HttpOnly only makes sense if the frontend does not read the cookie, how HttpHeaders gives all headers and cookies, and how to delete a cookie with Max-Age 0 on the same path.
Open with
I would inject the header with @HeaderParam X-Request-Id as a String and the cookie with @CookieParam session, either as a jakarta.ws.rs.core.Cookie or as a String if I only need the value, and I would treat both as possibly null.
Then, if they want more
For the response I would use NewCookie.Builder, which is available from Jakarta REST 3.1: name theme, value dark, path slash, maxAge thirty days in seconds, which is 2,592,000, secure true, httpOnly true and sameSite LAX. Then I return Response.ok with the entity and add the cookie with .cookie(theme).
On older JAX-RS versions there was no builder and no SameSite support, so I would check the version.
I would also explain the HttpOnly choice: it hides the cookie from JavaScript, which is essential for a session cookie, but a theme is not a secret, and if the frontend reads it to apply dark mode, HttpOnly would break that, so I would drop it in that case and keep Secure.
Header names are case-insensitive, and if I need everything I can inject HttpHeaders with @Context. To delete a cookie, I send the same name and path with Max-Age zero.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Jakarta EE 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a JAX-RS method that reads a header X-Request-Id and a cookie named session, and...": https://scrivio-interview-library.netlify.app/java/jax-rs/247a24a1cf/