注册页面register.html包括登录名、密码、姓名、性别、邮箱、手机号等信息,要求对所有字段进行非空判断,密码为数字、字母和特殊字符的组合(不少于8位),对邮箱和手机号进行有效性判断,否则弹出对话框进行提示。注册信息处理由映射地址为register.do的RegisterServlet.java进行处理,将所有用户注册的登录名、密码、姓名、性别、邮箱、手机号等信息按行保存到userinfo.txt中,每个字段用“|”分隔,要求将密码以SHA256加密(可用JDK自带的java.security.MessageDigest实现)后写入文件
时间: 2023-05-12 11:03:25 浏览: 216
带验证码的登录页面.rar
ChatGPT: 您好,我可以帮您回答这个问题。对于注册页面register.html,可以使用JavaScript进行非空判断和有效性判断,比如使用正则表达式对邮箱和手机号进行验证。在RegisterServlet.java中,可以使用BufferedWriter将用户信息写入userinfo.txt文件中,并使用java.security.MessageDigest对密码进行SHA256加密。以下是一个可能的实现:
JavaScript代码:
```javascript
function validateForm() {
var username = document.forms["registerForm"]["username"].value;
var password = document.forms["registerForm"]["password"].value;
var name = document.forms["registerForm"]["name"].value;
var gender = document.forms["registerForm"]["gender"].value;
var email = document.forms["registerForm"]["email"].value;
var phone = document.forms["registerForm"]["phone"].value;
if (username == "" || password == "" || name == "" || gender == "" || email == "" || phone == "") {
alert("所有字段都是必填的!");
return false;
}
var passwordRegex = /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{8,}$/;
if (!passwordRegex.test(password)) {
alert("密码必须是数字、字母和特殊字符的组合(不少于8位)!");
return false;
}
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
alert("请输入有效的邮箱地址!");
return false;
}
var phoneRegex = /^1[3456789]\d{9}$/;
if (!phoneRegex.test(phone)) {
alert("请输入有效的手机号码!");
return false;
}
return true;
}
```
Java代码:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class RegisterServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
// SHA256加密密码
String encryptedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
byte[] digest = md.digest();
encryptedPassword = String.format("%064x", new java.math.BigInteger(1, digest));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// 将用户信息写入文件
try (BufferedWriter writer = new BufferedWriter(new FileWriter("userinfo.txt", true))) {
writer.write(username + "|" + encryptedPassword + "|" + name + "|" + gender + "|" + email + "|" + phone);
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文