jsp网页转发+请求

这是一个关于网页登录验证的示例,包括登录页面和验证页面。登录页面包含用户名和密码输入框,验证页面检查输入是否匹配预设的用户名和密码。如果验证成功,使用请求转发到欢迎页面,并将用户名存储在request和session中;失败则显示错误提示并重定向回登录页面。
摘要由CSDN通过智能技术生成

 登录页面

<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<form action="index.jsp">
		用户名:<input type="text" name="uname">
		<br>
		密码:<input type="password" name="upwd">
		<hr>
		<input type="submit" value="登录">
		<input type="reset" value="重置">
	</form>
</body>
</html>

登陆验证页面,在这里可以连接数据库

<%
	//接受数据
	//接受用户名
	String uname=request.getParameter("uname");
	//接受密码
	String umm=request.getParameter("mm");
	//连接数据库
	/*
	request:请求
	response:响应
	session:会话
		会话开始:打开浏览器
		会话结束:关闭浏览器
		application(服务器级):
	跳转页面:
		转发:request.getRequestDispatcher("index.jsp").forward(request,response);
			转发带有数据:直接把请求对象和响应对象转发给目标页面
			传递数据有效范围内:在两个页面之间有效
		重定向:
			response.sendRedirect("index.jsp");
			重定向方式 跳转页面不带有数据
	*/
	if(uname.equals("admin")&&"123".equals(umm)){
		//js跳转方式
		//out.print("<script>location.href='index.jsp'</script>");
		//转发
		request.getRequestDispatcher("index.jsp").forward(request,response);
		//把用户名放到request中
		request.setAttribute("a", uname);
		request.setAttribute("bbb", "吊塔");

		//重定向
		//response.sendRedirect("index.jsp");
	}else{
		//js跳转
		out.print("<script>alert('账号或密码错误');location.href='login.jsp'</script>");
	}
%>

转发页面一:index,使用request

<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<h1>欢迎:<%=request.getParameter("uname") %></h1>
	<h1>欢迎:<%=request.getAttribute("a") %></h1>
	<h1>欢迎:<%=session.getAttribute("bbb") %></h1>
	
	<h2>
		<a href="index2.jsp">走你</a>
	</h2>
</body>
</html>

 转发页面二:index2

<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<h2>欢迎老弟来<%=request.getParameter("uname") %></h2>
	<h2>欢迎老弟来<%=request.getAttribute("a") %></h2>
	<h2>欢迎老弟来<%=session.getAttribute("bbb") %></h2>
	
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值