JSP from shallow to deep (9) — JSP sessions

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 of each user. For this purpose, we can use "sessions" in JSP.

Session is a user related object. When users visit the website, some data is stored in the session and taken out when needed. Session saves data for different users.

The following page places the user's name in the session and can display it elsewhere. First, we'll make a form, and then name it getname html

What's your name? < INPUT TYPE=TEXT NAME=username SIZE=20>

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

<%

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

session. setAttribute( "theName",name );

%>

Continue

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:

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

If you open two different browsers or run two browsers from two different machines, you can put one name in one browser and another name in the other browser, but both names will be tracked. The session keeps tracking until it times out. At this time, it will assume that the user has not visited the website, so it cancels the session.

Finally, let's arrange the connection of this tutorial: using session, add a user's "age" attribute in the above example.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>