Java- Cookies questions
Q.What is Cookies and what is the use of Cookies ?
Ans: Cookies are used to get user agents (web browsers etc) to hold small
amounts of state associated with a user’s web browsing.Later that
infromation read by server
Q. What are cookies and how will you use them?
Ans: Cookies are a mechanism that a servlet uses to have a client hold a
small amount of state-information associated with the user.
a) Create a cookie with the Cookie constructor:
public Cookie(String name, String value)
b) A servlet can send a cookie to the client by passing a Cookie
object to the addCookie() method of
HttpServletResponse:
public void HttpServletResponse.addCookie(Cookie cookie)
c) A servlet retrieves cookies by calling the getCookies() method of
HttpServletRequest:
public Cookie[ ] HttpServletRequest.getCookie( ).
Q.How many Cookies is supported to the host ?
Ans: User agents excepted to support twenty per host.And its take four
Kilobytes each.
Q.What is the use of setComment and getComment methods in Cookies ?
Ans: setComment:If a user agent (web browser) presents this cookie to a user, the cookie’s purpose will be described using this comment. This is not supported by version zero cookies.
public void setComment(String use)
{
}
getComment() :
Returns the comment describing the purpose of this cookie, or null if no such comment has been defined.
Q.Why we are used setMaxAge() and getMaxAge() in Cookies ?
Ans: setMaxAge
public void setMaxAge(int expiry)
Sets the maximum age of the cookie.The cookie will expire after that many seconds have passed.Negative values indicate the default behaviour:the cookie is not stored persistently, and will be deleted when the user agent exits.A zero value causes the cookie to be deleted
getMaxAge():
public int getMaxAge()
Returns the maximum specified age of the cookie. If none was specified, a negative value is returned, indicating the default behaviour described with setMaxAge.
Q.What is the use of setSecure() and getSecure() in Cookies ?
Ans: setSecure
Indicates to the user agent that the cookie should only be sent using a secure protocol (https). This should only be set when the cookie’s originating server used a secure protocol to set the cookie’s value.
public void setSecure(boolean flag)
getSecure() :
It Returns the value of the ’secure’ flag.
-
Method signature :
public boolean getSecure()




Leave a Reply
You must be logged in to post a comment.