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.