前置知识:
MVC模式,不懂的戳我哦
View:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- View 部分 -->
<form action="LoginServlet" method = "post">
<input type = "text" name = "uname">
<input type = "password" name = "upwd">
<input type = "submit" value = "登录">
</form>
</body>
</html>
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.print("登录成功");
%>
</body>
</html>
Controller:
LoginServlet.java
package org.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dao.LoginDao;
import org.entity.Login;
// 控制器实行分发
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 根据内置对象得到用户的信息
String name = request.getParameter("uname");
String password = request.getParameter("upwd");
// 将用户信息封装成一个实体类,传递给模型层
Login login = new Login(name,password);
LoginDao loginDao = new LoginDao();
// 传输一个JavaBean 封装的实体类
int cnt = loginDao.login(login);
// 根据从模型层返回的信息反馈给 View,执行具体的操作
if(cnt > 0) {
// 校验成功 跳转到欢迎界面
response.sendRedirect("welcome.jsp");
} else {
// 校验失败 跳转到 登录界面 进行重新登录
response.sendRedirect("login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Model:
LoginDao.java -- 封装业务逻辑的 JavaBean
JDBC相关知识的 Click Me
package org.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.entity.Login;
// 模型层(封装逻辑或者封装实体类 -- 数据 , 实现某种功能)
public class LoginDao {
private static String URL = "jdbc:mysql://localhost:3306/sqltest";
private static String User = "root";
private static String Password = "root";
//封装业务逻辑,实现校验用户信息的功能
public int login(Login login) {
Connection connection = null;
PreparedStatement pstam = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(URL, User, Password);
String sql = "select count(*) from login where uname = ? && upwd = ?";
pstam = connection.prepareStatement(sql);
pstam.setString(1,login.getName());
pstam.setString(2,login.getPassword());
rs = pstam.executeQuery();
if(rs.next()) {
return 1;
} else {
return 0;
}
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(pstam != null) pstam.close();
if(connection != null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
return -1;
}
}
Login.java -- 封装数据的 JavaBean (对应数据库中的一张表)
package org.entity;
// 封装数据的 JavaBean,对应数据库中的一张表;
public class Login {
private String name;
private String password;
// 无参的构造方法
public Login() {
}
// 带参的构造方法
public Login(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
后记:
一定要有自信,不要让怯懦成为你前进路上的畔脚石。