JSP内置对象session

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

session在网络中被称为会话。由于HTTP协议是一种无状态协议,为了弥补这一缺点,HTTP协议提供了session。通过session可以在应用程序的Web页面间进行跳转时,保存用户的状态,使整个用户会话一直存在下去,直到关闭浏览器。如果在一个会话中,客户端长时间不向服务器发出请求,session对象会自动消失。这个时间取决于服务器,Tomcat默认时间30分钟,可以进行更改。

1、创建和获取客户的会话

<% session.setAttribute("name", "lili");%>

<% session.getAttribute("name");%>

2、从会话中移除指定的绑定对象

<% session.removeAttribute("name");%>

3、销毁session

<% session.invalidate();%>

4、会话超时的管理

在session对象中提供了设置会话生命周期的方法,分别介绍如下

5、应用

直接上代码:

index.jsp:

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>起始页</title>
</head>
<body>

	<form name="form1" id="form1" method="post" action="session.jsp">
		<div align="center">
			<table width="23%" border="0">
				<tr>
					<td width="36%">
						<div align="center">您的名字是:</div>
					</td>
					<td width="64%"><label>
							<div align="center">
								<input type="text" name="name" />
							</div>
					</label></td>
				</tr>

				<tr>
					<td colspan="2"><label>
							<div align="center">
								<input type="submit" name="Submit" value="提交" />
							</div>
					</label></td>
				</tr>
			</table>
		</div>
	</form>


</body>
</html>


session.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%
	    String name = request.getParameter("name");
	    session.setAttribute("name", name);
	%>

	<form name="form1" id="form1" method="post" action="result.jsp">

		<table width="28%" border="0">
			<tr>
				<td>您的名字是:</td>
				<td><%=name%></td>
			</tr>

			<tr>
				<td>您最喜欢去的地方是:</td>
				<td><label> <input type="text" name="address" />
				</label></td>
			</tr>

			<tr>
				<td colspan="2"><label>
						<div align="center">
							<input type="submit" name="Submit" value="提交" />
						</div>
				</label></td>
			</tr>
		</table>

	</form>

</body>
</html>


result.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%
	    String name = session.getAttribute("name").toString();
	    String solution = request.getParameter("address");
	%>

	<form id="form1" name="form1" method="post" action="">

		<table width="28%" border="0">
			<tr>
				<td colspan="2"><div align="center">
						<strong>显示答案</strong>
					</div></td>
			</tr>

			<tr>
				<td width="49%"><div align="left">您的名字是:</div></td>
				<td width="51%"><label>
						<div align="left"><%=name%></div>
				</label></td>
			</tr>
			<tr>
				<td><label>
						<div align="left">您最喜欢去的地方是:</div>

				</label></td>
				<td><div align="left"><%=solution%></div></td>
			</tr>
		</table>

	</form>


</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值