Entering content frame

Procedure documentation Setting Cookies in HTTP Servlets Locate the document in its SAP Library structure

Prerequisites

To be able to set a cookie to the HTTP response object, you must first create it. You do this using the Cookieconstructor that takes the cookie name and cookie value as parameters.

Example

This example creates a cookie called MyFirstCookie with value of 1.

Cookie SapCookie = new Cookie("MyFirstCookie", "1");

Procedure

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.

For more information about the methods that you can use to set attributes to a cookie, see the Cookie class methods of Java Servlet 2.3 API.

This graphic is explained in the accompanying text

SapCookie.setMaxAge(Integer.MAX_VALUE);

SapCookie.setComment( "Just an example" );

//Add the cookie to the response

response.addCookie(SapCookie);

Result

The Set-Cookie header is set to your HTTP response headers.

 

Leaving content frame