Login function of javaweb construction project management system (1)

  Beginning to learn javaweb, now the instructor asks to make a management system, after thinking about the idea and designing the database, I will first realize the login function. To use the database, you need to download the jar package of the database connection.

database:

public class DBConn {

	public static Connection getConnection() {

		Connection conn = null;
		try {
			// Register the driver com.mysql.jdbc.Driver
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Driver loaded successfully!");
			// get the connection
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/workproject", "root", "root");
			System.out.println("Get the connection successful!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Failed!");
			e.printStackTrace ();
		}
		return conn;
	}

	public static void CloseConn(Connection conn) {
		try {
			conn.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
	}
}


log in page:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>login</title>
</head>
<body>
<form action="WorkServlet?action=login" method="post">
	用户名:<input type="text" name="user"><br/><br/>
	密      码:<input type="password" name="pwd"><br/><br/>
	<input type="submit" value="登录">
	<input type="button" src="main.html" value="退出">
</form>
</body>

Since it is a javaweb project, I submit the form data to the servlet and select what function to perform by passing in a parameter.

String action = request.getParameter("action");
if (action.equals("login")) {
try {
				login(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}

Write a login method:

private void login(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		System.out.println("=============");
		// accept parameters
		String user = request.getParameter("user");
		user = (user == null) ? "" : user;
		String pwd = request.getParameter("pwd");
		pwd = (pwd == null) ? "" : pwd;
		System.out.println("Username: " + user + "Password: " + pwd);

		
		WorkDaoimpl wd2 = new WorkDaoimpl();

		int result = wd2.loginfind(user, pwd);

		if (result == 0) {
			try {
				// request.getRequestDispatcher("index.jsp").forward(request, response);
				PrintWriter out = response.getWriter();
				out.print("<script language='javascript'>alert('登录成功!');window.location.href='index.jsp';</script>");

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}

		} else {
			try {
				// request.getRequestDispatcher("login.html").forward(request, response);
				PrintWriter out = response.getWriter();
				out.print(
						"<script language='javascript'>alert('The account or password is incorrect, please log in again!');window.location.href='login.html';</script>");

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace ();
			}
		}

	}

The implementation class code of the dao layer:

public int loginfind(String user1, String pwd1) {
		int b = 0;

		// get the connection
		Connection conn = DBConn.getConnection();

		// create sql statement
		String sql = "select * from managers where user=? and pwd=?";

		// Create Preparedstatement object

		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setObject(1, user1);
			pstmt.setObject(2, pwd1);
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				b=0;
			}else {
				b=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		return b;

	}
}

Among them, I use the rs.next() method to judge whether the data entered during login is the same as the data in the database. If it is true, it proves that the database has this data, and the login is successful. On the contrary, the login fails.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599600&siteId=291194637