用Myeclipse建立一个工程, 实现页面注册 利用<jsp:include>设计注册页(注册信息包括学号、用户名、密码、密码确认、邮件。同时包含访问量统计的功能(利用application对象)) 利用JSP设计注册处理页(验证学号是否为10位,密码与密码确认是否相同,邮件地址是否正确)。 不正确跳回注册页面(利用<jsp:forward>,<jsp:param>),处理成功后跳转到注册成功页(利用response对象),并显示所输入的用户名(中文)与密码(利用session对象)。
时间: 2023-12-14 15:39:54 浏览: 30
首先,你需要在MyEclipse中创建一个新的Web项目,然后在该项目下创建一个名为 "register" 的JSP页面。
1. 在 "register.jsp" 页面中,使用 <jsp:include> 标签引入 "header.jsp" 和 "footer.jsp" 页面,并在页面中添加一个表单用于用户注册。表单中包含以下输入项:
- 学号
- 用户名
- 密码
- 密码确认
- 邮件
同时,在表单下方添加一个统计访问量的功能,利用application对象实现。
2. 在 "register.do" 页面中,使用 JSP 代码验证用户输入的信息是否正确。验证包括以下内容:
- 验证学号是否为10位
- 验证密码与密码确认是否相同
- 验证邮件地址是否正确
若验证不通过,则使用 <jsp:forward> 标签跳回 "register.jsp" 页面,并使用 <jsp:param> 标签传递错误信息给 "register.jsp" 页面。
3. 若验证通过,则使用 response 对象跳转到 "success.jsp" 页面,并在页面中显示用户输入的用户名和密码。此时需要使用 session 对象将用户名和密码保存起来,以便在 "success.jsp" 页面中显示。
下面是示例代码:
register.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>
<jsp:include page="header.jsp" />
<h1>用户注册</h1>
<form action="register.do" method="post">
学号:<input type="text" name="studentId"><br>
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="confirmPassword"><br>
邮件:<input type="text" name="email"><br><br>
<input type="submit" value="注册">
</form>
<hr>
<h3>访问量统计: <%= application.getAttribute("count") %></h3>
<% int count = (int) application.getAttribute("count");
application.setAttribute("count", count + 1); %>
<jsp:include page="footer.jsp" />
</body>
</html>
```
register.do:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String studentId = request.getParameter("studentId");
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirmPassword = request.getParameter("confirmPassword");
String email = request.getParameter("email");
// 验证学号是否为10位
if (studentId.length() != 10) {
request.setAttribute("errorMsg", "学号必须为10位!");
request.getRequestDispatcher("register.jsp").forward(request, response);
}
// 验证密码和确认密码是否相同
if (!password.equals(confirmPassword)) {
request.setAttribute("errorMsg", "密码和确认密码不相同!");
request.getRequestDispatcher("register.jsp").forward(request, response);
}
// 验证邮件格式是否正确
if (!email.matches("\\w+@\\w+\\.\\w+")) {
request.setAttribute("errorMsg", "邮件格式不正确!");
request.getRequestDispatcher("register.jsp").forward(request, response);
}
// 保存用户名和密码到session中
session.setAttribute("username", username);
session.setAttribute("password", password);
response.sendRedirect("success.jsp");
%>
```
success.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>
<h1>恭喜你,注册成功!</h1>
<p>用户名: <%= session.getAttribute("username") %></p>
<p>密码: <%= session.getAttribute("password") %></p>
</body>
</html>
```