用JSP实现1.编写注册页面,供新用户注册,可填写用户名、密码、性别、出生日期、email、手机号码、收货地址。2.编写登录页面,输入注册好的账号和密码,则登录成功,到达另一个新页面。若输入的账号、密码错误,则显示“输入错误,请重新登录!” 。
时间: 2024-04-29 14:23:43 浏览: 14
注册页面(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>
<h1>用户注册</h1>
<form action="register.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
性别:
<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br>
出生日期:<input type="date" name="birthday"><br>
邮箱:<input type="email" name="email"><br>
手机号码:<input type="tel" name="phone"><br>
收货地址:<input type="text" name="address"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
注册处理页面(register.do):
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
String birthday = request.getParameter("birthday");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
String url = "jdbc:mysql://localhost:3306/mydb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
String user = "root";
String password_db = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password_db);
Statement stmt = conn.createStatement();
String sql = "INSERT INTO user (username, password, gender, birthday, email, phone, address) VALUES ('"
+ username + "', '" + password + "', '" + gender + "', '" + birthday + "', '" + email + "', '"
+ phone + "', '" + address + "')";
stmt.executeUpdate(sql);
out.print("<h1>注册成功!</h1>");
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.print("<h1>注册失败!</h1>");
}
%>
```
登录页面(login.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>
<form action="login.do" method="post">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
登录处理页面(login.do):
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String url = "jdbc:mysql://localhost:3306/mydb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
String user = "root";
String password_db = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password_db);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM user WHERE username='" + username + "' AND password='" + password + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
response.sendRedirect("welcome.jsp");
} else {
out.print("<h1>输入错误,请重新登录!</h1>");
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.print("<h1>登录失败!</h1>");
}
%>
```
欢迎页面(welcome.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>
</body>
</html>
```