JSP (9) -- JSP sessions

Source: Internet
Author: User

On a classic website, visitors can browse several webpages and perform some interactive activities. If you are writing such a website, it is very useful to use some data of each user. For this purpose, we can use "sessions" in JSP ".

Session is a user-related object. When a user accesses a website, some data is stored in the session and retrieved as needed. Session stores different data for different users.

The following webpage places the user's name in the session and displays it elsewhere. First, we want to create a table, and then name it getname.html.

<HTML>

<Body>

<Form method = post action = "savename. jsp">

What's your name? <Input type = text name = username size = 20>

<P> <input type = submit>

</Form>

</Body>

</Html>

The target of this form is "savename. jsp", which saves the user name in the session.

<%

String name = request. getparameter ("username ");

Session. setattribute ("thename", name );

%>

<HTML>

<Body>

<A href = "nextpage. jsp"> continue </a>

</Body>

</Html>

Savename. jsp saves the user's name in the session and connects to another web page nextpage. jsp. Nextpage. jsp shows how to retrieve the saved Name:

<HTML>

<Body>

Hello, <% = session. getattribute ("thename") %>

</Body>

</Html>

If you open two different browsers or run two browsers on two different machines, you can place a name in one browser, put another name in another browser, but both names will be tracked. The session keeps track until it times out. In this case, the session is canceled if the user has not accessed the website.

Finally, assign the contact information for this tutorial: Use session to add a user's "Age" attribute in the above example.

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.