JSP内置对象简单应用

JSP内置对象简单应用

登录页面
在这里插入图片描述

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'Login.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>

		<%
			String message = "";
			// 获取刚刚那个页面设置message的值
			message = (String) request.getAttribute("message");
			if (message == null) {
				message = "";
			}
		%>
		<form action="reg.jsp" method="post">
			用户名:
			<input type="text" name="username">
			<br />
			密 码:
			<input type="password" name="pwd" />

			<%=message%>
			<br />
			<input type="submit" values="登录" />
		</form>
		
	</body>
</html>

账号或密码错误页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'success.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
	<center>
		<%
			// 中文编码
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			//获取请求值
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			
			if("admin".equals(username)&&"123456".equals(pwd)){
			// 设置属性值   以便跳转到下个页面获取
			session.setAttribute("username",username);
			session.setAttribute("pwd",pwd);
			//跳转页面  直接跳转
			response.sendRedirect("success.jsp");
			
			}else{
			//跳转页面  间接跳转
			request.setAttribute("message","用户名或密码错误");
			
			request.getRequestDispatcher("Login.jsp").forward(request,response);
			}
		%>
		</center>
	</body>
</html>

登录成功页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'index.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
	<center>
	<p style="color:red; font-size:20px">登陆成功</p>
		<%
			// 中文编码
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			//获取属性值   (String)强行转换数据类型
			String username = (String)session.getAttribute("username");
			String pwd = (String)session.getAttribute("pwd");

			out.println("用户名:" + username);%>
			<br/>
			<%
			out.println("密    码:" + pwd);
		%>
	</center>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值