基于Servlet与JDBC的注册登录系统设计: 1)制作一个学生注册的页面,提交给RegServlet组件处理,能够输入如下数据项: 学号,姓名,昵称,性别,出生日期,所在院系,所选专业,班级,体育爱好(从篮球、足球、排球、羽毛球、乒乓球中多选),音乐爱好(可从钢琴、小提琴、吉他、古筝中多选),英语水平(可从优良、中等、一般中单选)。 2)在RegServlet中获取用户输入数据,并借助JDBC技术,将注册信息保存到数据库中;最后,跳转到注册成功页面,显示该用户的所有注册信息。 3)制作一个用户登录的页面,能够输入用户名、密码登录,表单提交给LoginServlet处理。 4)在LoginServlet中处理用户登录请求,获取输入的用户名和密码,并使用JDBC连接数据库进行验证; 若验证成功,则进入成功页面,显示欢迎信息以及登录的用户名;若验证失败,则进入登录失败页面,提示用户重新登录,并提供超链接。 5)使用数据源改进数据库连接部分的代码,获得更高的数据库访问效率。的代码
时间: 2023-12-10 13:05:46 浏览: 83
以下是基于Servlet与JDBC的注册登录系统的代码:
RegServlet.java
```
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 RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的数据
String id = request.getParameter("id");
String name = request.getParameter("name");
String nickname = request.getParameter("nickname");
String gender = request.getParameter("gender");
String birthday = request.getParameter("birthday");
String department = request.getParameter("department");
String major = request.getParameter("major");
String clazz = request.getParameter("clazz");
String[] sports = request.getParameterValues("sports");
String[] musics = request.getParameterValues("musics");
String englishLevel = request.getParameter("englishLevel");
// 将用户输入的信息保存到数据库中
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO student (id, name, nickname, gender, birthday, department, major, clazz, sports, musics, english_level) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, nickname);
ps.setString(4, gender);
ps.setString(5, birthday);
ps.setString(6, department);
ps.setString(7, major);
ps.setString(8, clazz);
ps.setString(9, String.join(",", sports)); // 将体育爱好用逗号分隔拼接成字符串
ps.setString(10, String.join(",", musics)); // 将音乐爱好用逗号分隔拼接成字符串
ps.setString(11, englishLevel);
ps.executeUpdate();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 跳转到注册成功页面,并显示该用户的所有注册信息
request.setAttribute("id", id);
request.setAttribute("name", name);
request.setAttribute("nickname", nickname);
request.setAttribute("gender", gender);
request.setAttribute("birthday", birthday);
request.setAttribute("department", department);
request.setAttribute("major", major);
request.setAttribute("clazz", clazz);
request.setAttribute("sports", String.join(",", sports));
request.setAttribute("musics", String.join(",", musics));
request.setAttribute("englishLevel", englishLevel);
request.getRequestDispatcher("/regSuccess.jsp").forward(request, response);
}
}
```
LoginServlet.java
```
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;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 使用JDBC连接数据库进行验证
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "123456";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pwd);
String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
// 验证成功,跳转到成功页面
request.setAttribute("username", username);
request.getRequestDispatcher("/loginSuccess.jsp").forward(request, response);
} else {
// 验证失败,跳转到失败页面
response.sendRedirect("/loginFail.jsp");
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
使用数据源改进数据库连接部分的代码
```
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取用户输入的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 使用数据源连接数据库进行验证
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/test");
conn = ds.getConnection();
String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
// 验证成功,跳转到成功页面
request.setAttribute("username", username);
request.getRequestDispatcher("/loginSuccess.jsp").forward(request, response);
} else {
// 验证失败,跳转到失败页面
response.sendRedirect("/loginFail.jsp");
}
} catch (NamingException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文