W3schools - JSP_Cookie
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
Cookies
Are text files stored on the client computer and they are kept for various information tracking purposes
JSP transparently supports HTTP cookies using underlying servlet technology
3 Steps involved in identifying and returning users
-
Server script sends a set of cookies to the browser
-
Browser stores this information on the local machine for future use
-
When the next time the browser sends any request to the web server then it sends those cookies information to the server and server uses that information to identify the user
Cookies are usually set in an HTTP header (although JS can also set a cookie directly on a browser)
A JSP that sets a cookie might send headers
HTTP/1.1 200 OK
Date: Fri, 18 Feb 2022 11:44:38 GMT
Server : Apache/1.3.9 (UNIX) PHP / 4.0b3
Set-Cookie: name = xyz; expires = Friday, 18-Feb-21 12:44:38 GMT;
Path = /; domain = spongebob53.github.io
Connection: close
Content-Type: text/html
Name and value will be URL encoded
Expires field is an instruction to the browser to forget the cookie after the given time and date
If the browser is configured to store cookies, it will then keep this information until the expiry date
If the user points the browser at any page that matches the path and domain of the cookie,
It will resent the cookie to the server
Servlet Cookies method
setDomain(String pattern) : Sets the domain to which the cookie applies
getDomain() : Gets the domain to which the cookie applies
setMaxAge(int expiry) : Sets how much time (in seconds) should elapse before cookies expires. If you don’t set this, the cookie will last only for the current session
getMaxAge() : Returns the maximum age of the cookie, specified in seconds, By default -1 indicating the cookie will persist until the browser shutdown
getName() : Returns the name of the cookie. The name can’t be changed after the creation
setValue(String newValue) : Sets the value associated with the cookie
getValue() : Gets the value associated with the cookie
setPath(String uri) : Sets the path to which this cookie applies. If you don’t specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories
getPath() : Gets the path to which this cookie applies
setSecure(boolean flag) : Sets the Boolean value indicating whether the cookie should only be sent over encrypted(i.e, SSL) connections
setComment(String purpose) : Specifies a comment that describes a cookie’s purpose. The comment is useful if the browser presents the cookie to the user
getComment() : Returns the comment describing the purpose of this cookie, or null if the cookies has no comment
Set Cookies
- Creating a Cookie object
Cookie cookie = new Cookie(“key”, “value”);
- Setting the maximum age
Cookie.setMaxAge(60*60*24);
- Sending the Cookie into the HTTP response headers
response.addCookie(cookie);
Reading Cookies
<%
Cookie cookie = null;
Cookie[] cookies = null;
// Get an array of Cookies associated with the this domain
cookies = request.getCookies();
if( cookies != null ) {
for (int i = 0; i < cookies.length; i++) {
cookie = cookies[i];
out.print("Name : " + cookie.getName( ) + ", ");
out.print("Value: " + cookie.getValue( )+" <br/>");
}
} else {
out.println("<h2>No cookies founds</h2>");
}
%>
Delete Cookies
-
Read an already existing cookie and store it in Cookie object
-
Set cookie age as zero using the setMaxAge() to delete an existing cookie
-
Add this cookie back into the response header