To be able to set a cookie to the HTTP response object, you must first create it. You do this using the Cookie constructor that takes the cookie name and cookie value as parameters.
This example creates a cookie called MyFirstCookie with value of 1.
Cookie sapCookie = new Cookie("MyFirstCookie", "1"); |
You add the cookie to the browser using the addCookie(Cookie cookie) method of the HttpServletResponse object. Before you add the cookie, you can set various attributes to it, such as comments, the maximum time that the cookie can live before it expires, and so on.
The cookie attributes that are set using the set- methods of the javax.servlet.http.Cookie class that take parameters of type String should not contain Carriage Return/Line Feed (CR/LF) characters. If CR/LF are found, the AS Java throws com.sap.engine.services.httpserver.exceptions.HttpIllegalArgumentException to indicate the use of the illegal characters.
More information about the methods that you can use to set attributes to a cookie: the documentation of the javax.servlet.http.Cookie class in the Java Servlet 2.5 API specification.
sapCookie.setMaxAge(Integer.MAX_VALUE); sapCookie.setComment("Just an example"); //Add the cookie to the response response.addCookie(sapCookie); |
The Set-Cookie header is set to your HTTP response headers.