用户访问登录页面,输入用户名和密码后,点击登录。登录成功,则跳转到主页面,登录失败则跳转到登录页面,并在页面中显示错误提示语。
用户在登录页面中选择记住用户名和密码,则登录成功后,以后再次登录的时候就不需要输入用户名和密码了,点击登录按钮直接访问主页面。
使用Servlet+Cookie+Jdbc+MySQL完成!
新建数据库,库名为XXX,并创建XXX表。
表名:T_USER | ||
---|---|---|
字段名 | 类型 | 约束 |
id | int | 主键 |
username | varchar(100) | |
password | varchar(20) |
在WEB-INF包下创建lib并添加(有德鲁伊jar包,操作数据库的jar包)
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
public class Fengconnectionpool {
public static DruidDataSource dataSource=null;
static{
//1.获取数据源
dataSource = new DruidDataSource();
//1.2 基本4项
// 1) 驱动
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
// 2) 连接
dataSource.setUrl("jdbc:mysql://localhost:3306/javaweblogin?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
// 3) 用户
dataSource.setUsername("root");
// 4) 密码
dataSource.setPassword("123456");
//1.3 特殊项【可选】
// 1) 初始化大小
dataSource.setInitialSize(5);
// 2) 最大值活动数
dataSource.setMaxActive(10);
// 3) 最小空闲数
dataSource.setMinIdle(2);
}
//将获得连接也封装进一个方法,写成静态以后直接类名调用
public static Connection getConnection() {
try {
//2.获得连接
DruidPooledConnection conn = dataSource.getConnection();
return conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
//将资源的释放也封装进方法中
public static void close(PreparedStatement ps,Connection connection){
if (ps !=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection !=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public interface UserDao {
User getUserBy(String username,String password);
}
public class UserDaoImpl implements UserDao{
@Override
public User getUserBy(String username, String password) {
Connection connection=null;
PreparedStatement ps=null;
User user=null;
try {
connection = Fengconnectionpool.getConnection();
String sql="select * from login1 where username=? and password=?";
ps = connection.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String username1 = resultSet.getString("username");
String password1 = resultSet.getString("password");
user = new User(id, username1, password1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Fengconnectionpool.close(ps,connection);
}
return user;
}
}
4.7 编写service层接口及实现类
public interface UserService {
User login(String username,String password);
}
public class UserServiceImpl implements UserService {
private UserDao userDao=new UserDaoImpl();
@Override
public User login(String username, String password) {
return userDao.getUserBy(username,password);
}
}
@WebServlet("/loginPage")
public class LoginPageServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
if(req.getAttribute("msg") != null){
writer.write(""+req.getAttribute("msg")+"");
}
writer.write("");
writer.write("
"); writer.write("