JSP: share data on different pages of the same user

Source: Internet
Author: User

Http://blog.163.com/yangzhanghui_job/blog/static/17957506220127273348178/

COOKIE: the server saves user information on the client, such as the login name and password.
Purpose:
1. Save the user name and password. You do not need to log on again within a certain period of time.
2. record users' Website access preferences (for example, whether there is background music ).
3. Website personalization.

Sendredirdbms () method: This method transfers information to the next page.
Sendredirdbms ("welcome? Uname = shunping "); format:
Note: 1. Welcome indicates the servlet URL you want to jump.
2. Between the servlet URL name and variable? No.
3. To pass more than two values, separate them with the ampersands.
4. If you want to pass Chinese characters, you will get Garbled text and need to handle it.

You can use sendredirect () to pass a page information to another page.
Example: add the following code on the logon judgment page:
// Receiving username and password
String u = Req. getparameter ("username ");
String P = Req. getparameter ("password ");
// Jump to welcome
Res. sendredirect ("welcome? Uname = "+ u +" & upass = "+ p );
The following code is added to the welcome page:
// Get the user name passed from logincl
String u = Req. getparameter ("uname ");
String P = Req. getparameter ("upass ");
// Output to webpage
// Chinese Garbled text processing
Res. setcontenttype ("text/html; charset = GBK ");
Printwriter PW = res. getwriter ();
PW. Print ("Welcome to" + u + "Pass =" + p );

Share Data by hiding forms
The format is as follows:
<Form action = login>
<Input type = hidden name = A value = B>
</Form>

Session: when a user opens a browser and accesses a website, the server will allocate a space for the browser in the server's memory, and the space will be exclusive to the browser. This space is the session space. The default storage time is 30 min.
Purpose:
1. Shopping Cart in online shopping mall
2. save login user information
3. Put some data into the session for each page of the same user
4. Prevent Unauthorized user login to a page
......
You can think of a session as a table with two columns: attribute name and attribute value.
Session usage:
1. Get the session
Httpsession HS = request. getsession (true );
2. Add attributes to the session
HS. setattribute (string name, object Val );
3. Get an attribute from the session
String name = HS. getattribute (string name );
4. delete an attribute from the session
HS. removeattribute (string name );
Session processing location, in the XML file:
<Session-config>
<Session-Timeout> 30 </session-Timeout>
</Session-config>
Note:
1. When a browser accesses a website, the server will assign a unique session ID to the browser to distinguish different browsers.
2. All its attributes occupy the memory, so they are used as a last resort.

Example:
Prevent illegal user login
Add code in logincl. Java
// Write the successfully verified information to the session
// Obtain the session
Httpsession HS = Req. getsession (true );
// Modify the session time
HS. setmaxinactiveinterval (20 );
// Write attributes and values to the session
HS. setattribute ("pass", "OK ");
Welcome. Java Add code
// Obtain the session
Httpsession HS = Req. getsession (true );
String val = (string) HS. getattribute ("pass ");
// Judge
If (val = NULL)
{
Try
{
// Illegal Login
Res. sendredirect ("login ");
} Catch (exception E)
{
E. printstacktrace ();
}

}

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.