一、实验目的
通过编程和上机实验理解 JSP各个页面之间的响应和传递的过程。并且能够熟练的掌握JSP的内置对象的属性和方法,并能灵活运用。
二、实验要求
1. 基础练习:内置对象常用方法的使用。
2. 编写获取表单数据。
3. 按照下列要求实现简单注册、登录程序。
截图:
代码
注册页面 index.jsp
<%@ page language="java" contentType="text/html;charSet=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>用户注册页面</title>
</head>
<body>
<h2><font color="#844220">用户注册</font></h2>
欢迎你成为本网站的会员,请在下面表单填写注册信息
<fieldset>
<legend><h4 style="background:#844220"><font color="#ffffff"> 创建新账户</font></h4></legend>
<form action="register.jsp" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="user" value="输入用户名"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="password" name="repwd"/></td>
</tr>
<tr>
<td>电子邮件</td>
<td><input type="text" name="Email" /></td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>地址</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form></fieldset>
</body>
</html>
信息显示页面 register
<%@ 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=UTF-8">
<title>register success</title>
</head>
<body>
<h2><font color="#844220">注册信息</font></h2>
<form action="login.jsp">
<fieldset>
<legend><h4 style="background:#844220"><font color="#ffffff"> 用户注册信息</font></h4></legend>
<table>
<h4>
<%
request.setCharacterEncoding("utf-8");
//获取注册的user
String user=request.getParameter("user");
//把user用以"user"为关键字存储到session对象中
session.setAttribute("user", user);
//获取注册的password
String password=request.getParameter("pwd");
//把password用以"password"为关键字存储到session对象中
session.setAttribute("password",password);
//确认密码
String repassword=request.getParameter("repwd");
//邮箱
String email=request.getParameter("Email");
//手机
String phone=request.getParameter("phone");
//地址
String address=request.getParameter("address");
//输出信息
//判断两次密码是否相同,相同即注册成功
if(password.equals(repassword)){
out.println("用户名:"+user+"<br>");
out.println("密码:"+password+"<br>");
out.println("电子邮件:"+email+"<br>");
out.println("电话:"+phone+"<br>");
out.println("地址:"+address+"<br>");
out.println("<br><br><input type='submit' value='去登陆'>");
}
//不同则返回注册页面
else{
out.println("两次密码不一致!2秒后返回注册页面重新注册!");
out.println("<br><input type='button' value='确认'>");
response.setHeader("refresh", "2;url=index.jsp");
}
%>
</h4>
</table>
</fieldset>
</form>
</body>
</html>
登录界面 login.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=UTF-8">
<title>登录页面</title>
</head>
<body>
<h2><font color="#844220">我的账户</font></h2>
请登录
<form action="check.jsp">
<fieldset>
<legend><h4 style="background:#844220"><font color="#ffffff"> 登录</font></h4></legend>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user1"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td colspan="5" align="right"><input type="submit" value="login"></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
检查登录页面check.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=UTF-8">
<title>welcome</title>
</head>
<body>
<%
//注册的用户名
String username=(String)session.getAttribute("user");
//登录的用户名
String user1=request.getParameter("user1");
//注册用户名对应的密码
String password=(String)session.getAttribute("password");
//登录用户名对应的密码
String pwd=request.getParameter("pwd");
//判断该用户是否已注册
if(username==null||password==null){
out.println("该用户未注册!");
}else if(!(username.equals(user1)||password.equals(pwd))){
out.println("该用户未注册!");
}
//判断注册和登录的用户名和密码是否一致
else if(username.equals(user1)&&password.equals(pwd)){
out.println("欢迎您成功登陆!");
}
//失败
else{
out.println("账号或密码错误!2秒后返回登录页面!");
response.setHeader("refresh", "3;url=login.jsp");
}
%>
</body>
</html>