1.创建数据库
CREATE DATABASE USER;
2.使用USER表
USE USER;
3.建表
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32) UNIQUE NOT NULL,
PASSWORD VARCHAR(32) NOT NULL
);
1.Tomcat 服务器软件
2.Tomcat集成到IDEA中&部署web项目
3.项目布局
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录title>
<style>
div{
background-color: cornflowerblue;
}
h1{
color: lightpink;
}
style>
head>
<body >
<div align="center">
<form action="/Servlet" method="post">
<h1>用户登录页面h1>
<input type="text" name="username" placeholder="请输入用户名"><br>
<input type="password" name="password" placeholder="请输入密码"><br>
<br>
<input type="submit" value="登录">
form>
div>
body>
html>
2.百度网盘jar下载地址 提取码: e5wb
package cn.domain;
public class User {
//和数据库中对应,将来也可以增加作作注册页面
private int id;
private String username;
private String password;
//定义set get 方法
public int getId() {
return id;
}
public void setId(int 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;
}
//重写 toString
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///user
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
package cn.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtils {
/**
* 编写jdbc工具类
*/
private static DataSource ds;
static {
try {
//加载配置文件
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDateSource(){
return ds;
}
/**
* 获取连接connection对象
*/
public static Connection getConnnection() throws SQLException {
return ds.getConnection();
}
}
package cn.dao;
import cn.domain.User;
import cn.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDao {
/**
* 操作数据库user表的类
*/
//声明JDBCTemplate对象共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDateSource());//需要一个数据库连接池
public User login(User loginUser) {
try {
//编写sql
String sql = "select * from USER where username = ? and password = ?";
//调用query方法
User user = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class), loginUser.getUsername(), loginUser.getPassword());
return user;
} catch (DataAccessException e) {
e.printStackTrace();//记录日志
return null;
}
}
}
package cn.servletdome;
import cn.dao.UserDao;
import cn.domain.User;
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 java.io.IOException;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
//获取请求对象
String username = request.getParameter("username");
String password = request.getParameter("password");
//封装user对象
User user = new User();
user.setUsername(username);
user.setPassword(password);
//调用UserDao的login方法
UserDao userDao=new UserDao();
User login = userDao.login(user);
if (login == null){
//登录失败
request.getRequestDispatcher("/failServlet").forward(request,response);
}else {
//登录成功
//存储数据
request.setAttribute("user",user);
//登录成功
request.getRequestDispatcher("/SuccessServlet").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
package cn.servletdome;
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 java.io.IOException;
@WebServlet("/failServlet")
public class failServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
response.setContentType("text/html;charset=utf-8");
//输出
response.getWriter().write("登录失败,用户名或密码错误");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
package cn.servletdome;
import cn.domain.User;
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 java.io.IOException;
@WebServlet("/SuccessServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取request域中共享的user对象
User user = (User) request.getAttribute("user");
if (user != null) {
//给页面写一句话
//设置编码
response.setContentType("text/html;charset=utf-8");
//输出
response.getWriter().write("登录成功!" + user.getUsername() + ",欢迎您");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
super.doGet(request, response);
}
}