JSP内置对象之----session登录及注销

本文介绍了一个简单的登录验证过程实现,包括使用HTML表单收集用户名和密码,并通过JavaServlet进行验证。验证通过后,用户将被重定向到欢迎页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


验证操作顺序:

提交属性

取得属性

判断(空空判断 -->匹配 --> 成功--> 跳转欢迎页)否则()



<%@ page contentType = "text/html" pageEncoding= "GBK"%>
<html>
<head><title>登陆验证</title></head>
<body>
	<form action = "login.jsp" method = "post">
		用户名:<input type ="text" name = "uname"><br>
		密 码:<input type = "password" name = "upassword"><br>
			<input type = "submit" value ="提交">
			<input type = "reset" value ="重置">
	</form>

<%	
		String name = request.getParameter("uname");
		String password = request.getParameter("upassword");
		if (!(name==null || "".equals(name) 
				|| password==null || "".equals(password))){
			if ("zhangze".equals(name) && "lhj".equals(password)){
				response.setHeader("refresh", "3; URL = welcome.jsp");
				session.setAttribute("userid",name);
%>
					<h3>登录成功,三秒后跳转到欢迎页!</h3>
					<h3>如果浏览器无法跳转,请点击<a href="welcome.jsp"></a>这里</h3>
<%
			}else {
%>		
				<h3>错误的用户名或密码</h3>
<%
			}
		}	
%>
</body>
</html>

欢迎页welcome.jsp

<%@ page contentType ="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

<%@ page contentType ="text/html" pageEncoding ="GBK"%>
<html>
	<head>
		<title>注销页</title>
	</head>
<body>
<%
		response.setHeader("refresh", "3; URL = welcome.jsp");
		session.invalidate();
%>
<h3>您已经退出本系统,两秒后回到系统首页</h3>
<h3>如果没有跳转,请点击<a href="login.jsp"></a>这里</h3>
</body>
</html>

登录首页:


成功登录后的欢迎页:

注意问题:
1、登录表单属性提交到自己本页面
2、未加上“空空”判断后加载首页时就会出现非法用户错误登录:



3、跳转页问题要考虑浏览器无法跳转问题,而采用手工超链接跳转。
使用jsp内置对象实现登录注销功能可以通过以下步骤实现: 1. 创建登录页面,包含用户名和密码的输入框以及登录按钮。 2. 在登录页面中使用form表单提交数据到jsp页面。 3. 在jsp页面中使用request对象获取用户名和密码,并进行验证。 4. 如果验证通过,将用户信息保存到session对象中。 5. 在其他需要登录才能访问的页面中,使用session对象判断用户是否已经登录,如果没有登录则跳转到登录页面。 6. 在注销功能中,使用session对象清除保存的用户信息。 示例代码如下: 登录页面: ``` <form action="login.jsp" method="post"> <label>用户名:</label> <input type="text" name="username"><br> <label>密码:</label> <input type="password" name="password"><br> <input type="submit" value="登录"> </form> ``` login.jsp页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = request.getParameter("username"); String password = request.getParameter("password"); if("admin".equals(username) && "123456".equals(password)){ session.setAttribute("username", username); response.sendRedirect("index.jsp"); }else{ out.println("用户名或密码错误!"); } %> ``` 其他需要登录才能访问的页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = (String)session.getAttribute("username"); if(username == null){ response.sendRedirect("login.jsp"); } %> <html> <head> <title>需要登录才能访问的页面</title> </head> <body> <h1>欢迎访问需要登录才能访问的页面!</h1> <a href="logout.jsp">注销</a> </body> </html> ``` 注销页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% session.invalidate(); response.sendRedirect("login.jsp"); %> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值