使用内置方法的set/getAtrribute获取、传递参数来完成模拟登陆过程

本文详细介绍了使用JSP实现的模拟登陆流程,包括登陆界面、检查界面、成功与失败界面的代码示例,以及如何通过set/getAttribute方法进行参数传递,确保用户验证的准确性。

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

使用内置方法的set/getAtrribute获取、传递参数来完成模拟登陆过程

1、登陆界面代码

// Login.jsp
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="Check.jsp" method="setAtrribute">
	<input type="text" name="username"/><br>
	<input type="password" name="pwd"/><br>
	<input type="submit" value="submit"/>
	<input type="reset" value="reset"/>
	</form>
</body>
</html>

2、检查界面代码

// Check.jsp
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
	//
	String name=request.getParameter("username");
	session.setAttribute("username1", name);
	String password=request.getParameter("pwd");
	%>
	<%
	if(name.equals("Lewis")&&password.equals("123")){
		
	%><jsp:forward page="Success.jsp"></jsp:forward>
	<%
	}else{
	%>
	<jsp:forward page="Error.jsp"></jsp:forward>
	<%
	}
	%>
	
</body>
</html>

3、成功界面代码

//Success.jsp
    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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
	request.setCharacterEncoding("UTF-8");
	//String name=request.getParameter("username");
	String name=(String)session.getAttribute("username1");
	%>
	欢迎,<%=name%>登录成功!
</body>
</html>

4、失败界面代码

//Error.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	错误的用户名或密码
	<%
	request.setCharacterEncoding("UTF-8");
	//String name=request.getParameter("username");
	String name=(String)session.getAttribute("username1");
	//response.setHeader("refresh","3;url=Login.jsp"); %>
	<a href="Login.jsp">重新登录</a>
</body>
</html>

5、测试结果

设置了正确的登陆账号和密码分别为:Lewis、123,账号密码正确跳转到登陆成功界面,输出“欢迎Lewis用户登录成功!”,反之,跳转到登陆失败界面,可选择重新登录,也可以选择设置3秒钟后自动跳转回登录界面。
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值