通过JSP、JavaBean、JDBC、Servlet技术,实现用户登录功能
- 一、需求
- 二、代码开发
- 1、exam264Servlet类
- 2、exam264UserInfo类
- 3、exam264DBUtil类
- 三、完整demo源码
一、需求
通过JSP、JavaBean、JDBC、Servlet技术,实现用户登录功能:
1、创建JavaBean类(实体类、数据库连接类)
2、创建数据库表操作接口及相应的操作类
3、实现登录功能的Servlet类
4、创建登录页面(能够输入学号、密码) 、登录成功页面整个项目包括(但不限于) : 2个isp页面、1个Servlet类、2个JavaBean类、1个操作接口、1个操作类
二、代码开发
1、exam264Servlet类
package com.demo;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class exam264Servlet extends HttpServlet{public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{doPost(request, response);}public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{Integer userId = Integer.parseInt(request.getParameter("userId"));String pwd = request.getParameter("pwd");exam264UserInfo userInfo = new exam264UserInfo(userId,pwd);exam264Dao dao = new exam264DaoImpl();boolean isExist = dao.query(userInfo);if(isExist){response.sendRedirect("success.jsp");return;}try {request.getRequestDispatcher("/login.jsp").forward(request,response);} catch (ServletException e) {e.printStackTrace();}}
}
2、exam264UserInfo类
package com.demo;public class exam264UserInfo {private int userId;private String studentname;private String pwd;public exam264UserInfo() {}public exam264UserInfo(int userId, String pwd) {super();this.setUserId(userId);this.pwd = pwd;}public String getStudentname() {return studentname;}public void setStudentname(String studentname) {this.studentname = studentname;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}}
3、exam264DBUtil类
package com.demo;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class exam264DBUtil {// 数据库连接信息private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";private static final String URL = "jdbc:mysql://localhost:3306/demo";private static final String USERNAME = "root";private static final String PASSWORD = "root";// 加载数据库驱动static {try {Class.forName(DRIVER_CLASS);} catch (ClassNotFoundException e) {e.printStackTrace();}}// 获取数据库连接public static Connection getConnection() {Connection connection = null;try {connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);} catch (SQLException e) {e.printStackTrace();}return connection;}public static void closeResultSet(ResultSet resultSet) {try {if (resultSet != null) {resultSet.close();}} catch (SQLException e) {e.printStackTrace();}}public static ResultSet executeQuery(String sql,exam264UserInfo userInfo) {ResultSet res = null;Connection connection = null;PreparedStatement stat = null;try {connection = getConnection();stat = connection.prepareStatement(sql);stat.setInt(1,userInfo.getUserId());stat.setString(2, userInfo.getPwd());return stat.executeQuery();} catch (SQLException e) {e.printStackTrace();}return res;}
}
三、完整demo源码
https://download.csdn.net/download/weixin_43860634/87870884