JSP (9)--jsp Sessions

Source: Internet
Author: User
Tags sessions
Js|session on a classic website, visitors can browse several Web pages and perform some interactive behavior. If you are writing such a website, it is very useful to use some data from each user. For this purpose, we can use the "sessions" in the JSP.

Session is a user-related object. When a user visits a Web site, some data is stored in the session and extracted from the data when needed. The session saves different data for different users.

The following page places the user's name in the session and can display it elsewhere. First we're going to make a form 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>

    这个表单的目标是“SaveName.jsp”,它在session保存了用户的名字。

<%

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

session.setAttribute( "theName", name );

%>

<HTML>

<BODY>

<A HREF="NextPage.jsp">Continue</A>

</BODY>

</HTML>

SaveName.jsp在session保存了用户了名字,并连接到另外一个网页NextPage.jsp。NextPage.jsp 显示了怎样取出被保存的名字:

<HTML>

<BODY>

Hello, <%= session.getAttribute( "theName" ) %>

</BODY>

</HTML>

If you open two different browsers, or run two browsers from two different machines, you can place a name in one browser and a different name in the other browser, but two names will be tracked. The session remains tracked until it times out, and it assumes that the user has not visited the site, so it is canceled.

Finally, decorate this tutorial with the link: Use session, in the above example to add a user's "age" attribute.

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.