How to implement the data transfer between different pages to realize the data sharing of the page? There are 4 common ways to do this:
1) Form submission (form)
2) Sendredirect () jump
3) Session Technology
4) Cookie Technology
Form submission
This is the most common way and the simplest, but sometimes the technique works very well, like this:
<form action=login>
<input Type=text name=a value=b>
<input Type=hidden name=c value=d>
</form>
With the form, we can also pass a page information to another page. Pay special attention to hiding the application of the form, if you do not want to tell the information presented can be used.
Sendredirect () Jump
Sendredirect ("Welcome?uname=xiangkejin");
Advantage: The speed of transmitting information is relatively fast
Disadvantage: It can only transfer strings, not an object
Note the point:
1.welcome represents the URL of the servlet you want to jump to
What is the 2.servlet URL name and variable? No.
3. If you want to pass more than two values, they should be separated by A & number such as Sendredirect ("Welcome?uname=xiangkejin&password=ok");
4. If the delivery is in Chinese, it may be garbled, need to deal with
Session Technology
When a user opens a browser and accesses a Web site, the server assigns the browser a
Space, which is exclusive to the browser.
This space is the session space, the data in this space default time 30min, you can also modify.
Use:
1. Shopping cart in the online mall
2. Save logged-in user information
3. Put some data into the session for each page of the same user
4. Prevent users from illegally logging on to a page
The session can be seen as a table with two columns. Each row is a property of the session
Each property contains two parts, one is the name of the property (String) and the other is its value (Object)
How do I use the session?
Get the session ID, which is the container allocation, each browser occupies not belong to a machine client, is a request
String Sessionid=ht.getid ();
1. Get session
HttpSession hs=req.getsession (TRUE);
2. Adding attributes to the session
Hs.setattribute (String name,object val);
3. Get a property from the session
String Name=hs.getattibute (string name);
4. Delete a property from the session
Hs.removeattribute (String name);
So, delete all the sessions.
Hs.setmaxinactiveinterval (0);
Note for session:
1. Modify the default session time of 30min in Web. XML, or you can modify it in the program. Can be modified in Tomcat or modified at your own site
2. When a browser accesses a website, the server assigns a unique session ID to the browser, which distinguishes between different browsers
3. Because the various properties of the session occupy the memory of the server, software companies are using it in a compelling situation.
Cookie Technology
Cookies are information that the server saves the user on the client. This information is like cookies, the amount of data is small, the server can read from the client when needed
Generally stored in the C:\Documents and settings directory
Use:
1. Save the user name, password, and do not have to log in again for a certain time
2. Record the user's preferences for visiting the website
3. Personalization of the website, the service content of the customized website
How do I use cookies?
1.Cookie advantages like a table, divided into two columns, one is a name, one is a value
Data types are string
2. How to create a cookie (created on the server side)
Cookie C=new Cookie (String name,string val);
3. How to add a cookie to the client
Response.addcookie (c)
4. How to read cookies (from client to server)
Cookie [] cookie=request.getcookies ();
Other Notes:
1. You can enable or disable cookies by ie-tools-internet Options-Privacy-Advanced
2. Security is not high because cookie information is stored on the client
The life cycle of 3.cookie information can be set at creation time
The difference between a cookie and a session
1. Location of Presence
Cookies are saved on the client, session is saved on the server side
2. Security
The security of a cookie is weaker than the session. The cookie is stored in clear text and the session is stored in memory.
3. Network Transmission Volume
Cookies are transmitted through the network on the client and server side
Session saved on server side, no transfer required
4. Life cycle (20 minutes for example)
(1) The life cycle of a cookie is class, and it starts when it is created,
20 minutes after the end of the cookie life cycle.
(2) The life cycle of the session is spaced, starting from the time it was created, if in 20 minutes,
Did not visit the session, then the session information five small, if within 20 minutes,
After the session has been accessed, its life cycle will begin to compute again.
(3) In addition, the closing opportunity causes the session life cycle to end, but the cookie has no effect.
Servlet sharing data on different pages of the same user