JSP (9)

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">


Whats 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.