写一个javaweb 登录注册功能,基于maven,mysql,jsp,tomcat
时间: 2024-03-08 10:45:24 浏览: 276
用Java写的登录注册功能
5星 · 资源好评率100%
好的,我可以为您提供一个简单的JavaWeb登录注册功能的示例,基于Maven、MySQL、JSP和Tomcat。
步骤如下:
1. 创建Maven项目
在Eclipse或IntelliJ IDEA中创建一个新的Maven项目,选择Web应用程序类型,并在pom.xml文件中添加以下依赖关系:
```xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.31</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
</dependencies>
```
2. 创建数据库
在MySQL中创建一个名为“users”的数据库,其中包含一个名为“users”的表,其中包含以下列:id、username和password。
```sql
CREATE DATABASE users;
USE users;
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
```
3. 创建JSP页面
创建两个JSP页面,一个用于登录,一个用于注册。
login.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="login" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<input type="submit" value="Submit">
</form>
<p>Don't have an account? <a href="register.jsp">Register</a></p>
</body>
</html>
```
register.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="register" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<input type="submit" value="Submit">
</form>
<p>Already have an account? <a href="login.jsp">Login</a></p>
</body>
</html>
```
4. 创建Java Servlet
创建一个名为“LoginServlet”的Java Servlet,用于处理登录请求,并在Web应用程序的web.xml文件中进行配置。
LoginServlet.java:
```java
package com.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/users";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (authenticate(username, password)) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
private boolean authenticate(String username, String password) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement preparedStatement = connection
.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return true;
}
resultSet.close();
preparedStatement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
在web.xml文件中添加以下配置:
```xml
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
```
创建一个名为“RegisterServlet”的Java Servlet,用于处理注册请求,并在Web应用程序的web.xml文件中进行配置。
RegisterServlet.java:
```java
package com.example;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/users";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement preparedStatement = connection
.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
response.sendRedirect("login.jsp");
}
}
```
在web.xml文件中添加以下配置:
```xml
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.example.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
```
5. 创建欢迎页面
创建一个名为“welcome.jsp”的JSP页面,用于显示用户登录后的欢迎消息。
welcome.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome <%= session.getAttribute("username") %>!</h1>
<p><a href="logout.jsp">Logout</a></p>
</body>
</html>
```
6. 创建注销页面
创建一个名为“logout.jsp”的JSP页面,用于注销用户并从会话中删除用户名属性。
logout.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Logout</title>
</head>
<body>
<h1>You have been logged out.</h1>
<% session.removeAttribute("username"); %>
<p><a href="login.jsp">Login</a></p>
</body>
</html>
```
7. 配置Tomcat
将Web应用程序部署到Tomcat服务器,并启动Tomcat。在Web浏览器中打开http://localhost:8080 / login.jsp以访问登录页面。
这就是一个简单的JavaWeb登录注册功能示例,基于Maven、MySQL、JSP和Tomcat。
阅读全文