【jsp】session


利用session对象完成以下内容

login.jsp

完成登录表单的显示,同时想页面本身进行数据提交,以完成登录的验证,如果登录成功(用户名和密码固定:Joywy/1111),则保存属性;如果登录失败,则显示登录失败的信息。

  • login.jsp
<html>
<head><title>欢迎</title></head>
<body>
<form action="login.jsp" method="post">
	用户名:<input type="text" name="uname"><br>
	密   码:<input type="password" name="upass"><br>
	<input type="submit" name="登陆">
	<input type="reset" name="重置">
</form>
<%
	String name=(String).request.getParameter("uname");
	String password=(String).request.getParameter("upass");
	if(!(name==null || "".equals(name) || password==null || "".equals(password))){
		if("Joywy".equals(name) && "android".equals(password)){
			session.setAttribute("userid",name);
			response.setHeader("refresh","2,URL=welcome.jsp");
%>
<h3>登陆成功,即将跳转</h3>
<h3>若未跳转,点击<a href="welcome.jsp">这里</a></h3>
<%
		}else{
%>
			<h3>错误的用户名或密码!</h3>
<%
		}
	}
%>
</body>
</html>

Welome.jsp

此页面要求在用户登录完成之后才可以显示登录成功的信息,如果没有登录,则要给出未登录的提示,同时给出一个登陆的连接地址。

  • welcome.jsp
<% page contgentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎</title></head>
<body>
<%
	if(session.getAttribute("userid") != null){
%>
<h3>欢迎<%=session.getAttribute("userid")%>来到<a href="logout.jsp">注销</a></h3>
<%
	}else{
%>
	<h3>请先<a href="login.jsp">登录</a></h3>
<%
	}
%>
</body>
</html>

logout.jsp

此功能完成登录的注销,注销之后,页面要跳转会login.jsp,等待用户继续登录。

  • logout.jsp
<% page contgentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>欢迎</title></head>
<body>
<%
	response.setHeader("refresh","2,URL=login.jsp");
	session.invalidate();
%>
<h3>成功退出,即将返回首页</h3>	
<h3>如果没跳转,点击<a href="login.jsp">这里</a></h3>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cout0

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

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

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

打赏作者

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

抵扣说明:

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

余额充值