---Restoring content starts---
Cookies are text files stored on client computers and retain their various information tracking purposes. Java Servlet transparently supports HTTP cookies.
There are three steps to return the user when it comes to identification:
• A set of cookies sent by the server script to the browser. For example: name, age, or identification number, etc.
• The browser stores this information on the local computer for future use.
• The next time the browser sends any request to the web server, then these cookies are sent to the server, and the server will use this information to identify the user.
Here is a list of useful methods when you can use servlet to manipulate cookies.
| SN | Method & Description |
|---|---|
| 1 | public void setDomain(String pattern) This method sets the domain cookies to apply, for example www.yiibai.com |
| 2 | public String getDomain() This method gets the domain's cookie application, such as yiibai.com |
| 3 | public void setMaxAge(int expiry) This method sets the interval between how many times (in seconds) before the cookie expires. If not set this way, the cookie will continue only for the current session. |
| 4 | public int getMaxAge() This method returns the maximum age (period) cookie, specified in seconds, by default, -1 means that the cookie will continue until the browser closes. |
| 5 | public String getName() The name of the cookie returned by this method. The name after creation cannot be changed. |
| 6 | public void setValue(String newValue) This method sets the cookie value. |
| 7 | public String getValue() This method results in the cookie associated value. |
| 8 | public void setPath(String uri) This method sets the path to this cookie. If you do not specify the path, the cookie is returned by the same directory as all URLs in all subdirectories of the current page. |
| 9 | public String getPath() This method gets the path to this cookie. |
| 10 | public void setSecure(boolean flag) This method sets a boolean value to indicate whether the cookie should only send encrypted (such as SSL) connections. |
| 11 | public void setComment(String purpose) This method specifies the purpose of a cookie. Comments are very useful if the browser cookies are displayed to the user. |
| 12 | public String getComment() This method returns a comment that describes the purpose of this cookie or is null if the cookie has no comment. |
It's easy to use in a servlet, like this.
Cookie rmkeyCookie = new Cookie("RMKEY",useResVo.getRmkey()); rmkeyCookie.setMaxAge(3600); rmkeyCookie.setDomain(".why.com"); rmkeyCookie.setPath("/"); response.addCookie(rmkeyCookie);If there are multiple cookies, you can new multiple cookie objects, but in the client browser, there is still only one txt file, but there are only multiple key-value pairs.
Or so;
response.addHeader("set-cookie",StringUtil.appendString(cookieName, "=", cookieValue, ";Path=/;Domain=", domain, ";Max-Age=", maxAge, httpOnly));The above is the full content of the comprehensive understanding of how to use cookies in servlets that the editor brings to you. I hope everyone will support Wulin.com more~