Oracle Knowledge-http The Persistence state in the servlet

Source: Internet
Author: User
Tags bind session id rfc valid

Stateless of HTTP protocol

1. The advantages and disadvantages of stateless in the HTTP protocol

A The HTTP interaction is stateless

Stateless means that when the browser sends a request to the server, the server responds, but the same browser sends the request to the server, he responds, but he doesn't know that you are the browser.

b) Advantages

I. The client browser will not notice that the server fails and restarts.

II. It responds faster when the server does not need the prior information

c) Disadvantages

I. No memory capability for transactional processing, which may result in increased data delivery per connection

II. It is difficult to produce a set of pages that gather information to produce a good user experience.

2. http if there is the advantage of the state

A to reduce the amount of data transmitted

B Certification Web page can collect user's authentication information

c) You can define the user's access rights to the Web page

The principle and application of cookies

1. Cookies

A) Definition:

is a piece of information sent to a customer by the server, stored either inside the client browser or on a hard disk, and then sent back to the client in a subsequent request to the server.

b) Cookie Specification:

I. Netscape specification (version 0): http://wp.netscape.com/newsref/std/cookie_spec.html

Ii. RFC2109 (HTTP State Management mechanism, version 1): HTTP://WWW.IETF.ORG/RFC

C) Response header:

Set-cookie:name=value; Comment=value; Domain=value; Max-age=value; Path=value; Secure; Version=1*digit

D) Other

Cookies are made in Chinese and are invented by Netscape. Netscape is also the first company to develop browsers, and in the early days of the internet, Netscape has become the most popular browser with its preconceived advantages. But because of the popularity of Windows95, Microsoft's Internet Explorer and Windows to bind sales, Netscape gradually disappeared, not known. But most Internet protocols and regulations now originate in Netscape's products. Cookies are behind them. It is a fragment of information sent by the server to a customer that can be stored in the client's browser or on a hard disk, and then sent back to the client in a subsequent request to the server. Cookies currently follow the main specifications are two, one is Netscape specification, its specifications can be found in the http://wp.netscape.com/newsref/std/cookie_spec.html. The other is RFC2109, whose specification file can be found on HTTP://WWW.IETF.ORG/RFC. These two specifications specify the form of a header for a cookie in the HTTP protocol. For example, in the RFC2109 specification, the form of a response header is defined as such. Name is a cookie, and Vlue is its value. Name=value Property-value pairs must first appear, after which the property value pairs can appear in any order. The comment attribute is optional because cookies may contain information about the user's privacy, which allows the server to specify the use of the cookie, and the user can check the information and decide whether to join or resume the session. The domain attribute is optional to specify which field the cookie is valid in, and the field specified must start with the dot number. The Max-age property is optional, used to define the lifetime of the cookie, in seconds, if this time is exceeded, the client should discard the cookie, and if the specified number of seconds is 0, the cookie should be discarded immediately. The path attribute is optional and is used to specify which URL subset of this cookie is valid. The secure property is optional and has no value to instruct the browser to interact with the server in a secure manner. The Version property is required, and its value is a decimal integer that identifies the versions of the cookie according to the state management specification, which should be set to 1 for RFC2109 version.

2. Programming with Cookies

A) features

I. Record the contents of a session trace as a key-value pair

Ii. cookies have a higher durability

III. Users can manually restrict the use of cookies

Iv. session can be used to replace cookies

3. Set a cookie

A) Create cookies:

Cookie C = new Cookie ("MyName", "myvalue");

b The Cookie method:

I. getconnment (), setcomment (String purpose)

Ii. GetDomain (), SetDomain (String pattern)

Iii. getmaxage (), setmaxage (int expiry)

Iv. getName (), GetValue ()

V. SetValue (String NewValue)

Vi. GetPath (), SetPath (String URI)

c) Using cookies

I. Response.addcookie ()

Ii. Request.getcoonkie ()

4. Instance-Login procedure

A) function:

I. Use cookies to save user login information

Ii. Initial Login, save the information to the client's hard disk

Iii. re-access, first verify the user login information, through the display of welcome information

b) Steps:

I. First step: Preparation of Loginservlet.java and Greetservlet.java

Ii. Step Two: compiling Java source files

Iii. Step Three: Deploy the servlet

5. Example-loginserlvet

6. Example-greetservlet

7. Output session to Browser

A) Cookie

I. Tracking browser-related sessions

Ii. Some browsers do not support

III. Some users do not use for security reasons

b) overriding URLs

I. Bind a session to a browser through a session ID that requests a URL

Ii. time-consuming to scan page and URL redirection

Iii. use of Encodeurl () and Encoderedirecturl ()

The principle and application of Session

1. Session

A) Definition:

I. A browser to a collection of server-related HTTP interactions.

Ii. is the collection of data associated with those interactions that are obtained by the servlet accessed by the browser.

b) javax.servlet.http.HttpSession supported interfaces.

2. HttpSession interface

A) methods in the HttpSession interface

I. Public Object getattribute (String name)

Ii. public void setattribute (String name,object value)

Iii. public void RemoveAttribute (String name)

Iv. public int Setmaxinactiveinterval ()

V. public void invalidate ()

3. Supplementary notes

A The session is created by the servlet run.

b The servlet can get the session object in the following ways:

GetSession ()

C any servlet can request the creation of a session.

d) Access to information in the session is available to all servlet requests in the target browser.

E The session expires because the browser is inactive.

4. Cookie and Session comparison

A) session

I. Created by the server side

Ii. saving information from the service side

Iii. open a new browser and start a new session

b) Cookies

I. Fragment information sent by the server to the client, stored in the client browser's memory or on the hard disk.

Ii. saving information by the client

Iii. multiple browsers can access the same cookie object during the cookie validity period

Review cookies and session

Cookies store a variable and corresponding value in a browser.

Cookies can be configured by the browser user.

Not all browsers support cookies.

You can use the Addcookie () and GetCookie () methods to get cookies.

A session is a collection of data that is associated with an interaction between a browser and a network server.

Each browser opens and a new session object is created

The following methods exist during session development:

GetSession ()

SetAttribute (key, value)

GetAttribute (Key)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.