WebProgramDuring runtime, the servlet and Web Container need to communicate, and the servlet and Servlet also need to communicate. Httpservletrequest and servletrequest interfaces provide two methods: one is to set attributes, namely through servletrequest. setattribute (), servletrequest. getattribute (), and removeattribute. In this way, when the setvletrequest object is passed between Servlets, these attributes can be shared by these servlets. Another method is session ".
I. Concepts of sessions
Simply put, a session is a mechanism in which a Web Container associates a customer with a series of requests from the customer. Because the HTTP protocol itself is not maintained, an additional mechanism is required to meet the actual needs. What about this mechanism? For example, a Web container is like a cinema, and a browser customer is like a person watching a movie. They are associated with each other through movie tickets (this movie ticket also has a deadline ). Like watching a movie, a Web Client must present a "movie ticket" when requesting resources from the server ". This "movie pass" is typically represented by cookies. Web containers implement sessions in three ways: 1. COOKIE Mechanism 2. URL rewriting 3. Hiding form input. The cookie is a string of data sent by the Web server to the Web Client. When sending a request to the Web server, the browser must attach the cookie so that the server can authenticate the identity. URL rewriting is to set the sessionCodeMethod appended to the URL. The hidden form input is implemented through the hidden feature of the HTML form. Note that by default, there is no security mechanism for the three methods to implement sessions, and the cookie, URL, and hidden form data can be easily found. Therefore, if session encoding needs to be kept confidential, web programs should use security mechanisms such as encryption or SSL connections to ensure data security.
Ii. Process cookies
The servlet API's javax. servlet. http. Cookie class is used to create and process cookies. You can use the addcookie of httpservletresponse to send cookies to the browser. A cookie can have these attributes: name, value, comment, path, domain name, maximum age, version number, and so on. Among them, except the name and value, other families are optional. When the addcookie method is used to send a cookie to the browser, the following format of data is added to the HTTP Response Header:
Set-COOKIE: name = value; version = {version}; Comment = {comment}; domain = {domain}; max-age = {age}; Path = {path}
When the browser returns a request to the server again, it will attach the cookie sent by the server to the server. In this case, the web server can use httpservletrequest. the getcookie () method returns a cookie object.
The whole cookie processing process code is like this:
1. The server responds and generates cookies.
Cookie ck = new cookie ('ckname', 'ckvalue ');
Httpservletresponse. setcookie (CK );
2. The server receives browser requests.
Cookie [] CKS = httpservletrequest. getcookie ();
CKS [0]. getvalue ();
......
For more information about Cookie attributes and methods, see servlet javadoc.
3. httpsession
httpsession is a session that is implemented at the previous Layer Based on the cookie principle. An httpsession object represents a session. Its typical application scenarios are as follows:
Public void doget (httpservletresquest req, httpservletresponse resp) throws servletexception and ioexception
{< br> httpsession HS = req. getsession (true); // gets the session to which the current request belongs. If there is no session currently and the parameter is true, a new session is generated.
HS. getattribute ("atbtname"); // gets the value of atbtname, an attribute of the session HS.
HS. setattribute ("atbtname", atbtvalueobject); // adds or sets a session attribute, whose value is referenced by an object.
}< br> for more information about the httpsession interface, see javadoc.
one thing to note is that both cookie and httpsession can implement sessions, but there is a difference between the two. The difference is that httpsession is in a more abstract layer and easier to use. More importantly, the attribute of httpsession stores object references and is stored on the Web server. Therefore, it is more flexible to use. Cookies are stored on the browser, and different browsers have different restrictions on cookies, such as the number limit. Therefore, it is more intuitive and convenient to use httpsession to manage sessions.