jsp网页转发+请求

登录界面

<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>

        登录验证页面,可连接数据库

k<%
	//接受数据
	//接受用户名
	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>");
	}
%>

 转发页面index1.jsp

<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.jsp

<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>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值