<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logintitle>
head>
<body>
<form action="/demo/login" method="post">
用户名 <input type="text" name="username" placeholder="请输入用户名"><br>
密码 <input type="password" name="password" placeholder="请输入密码"><br>
<input type="submit" value="登录">
form>
body>
html>
2). 需要导入的配置文件,jar包
commons-logging-1.2.jar
druid-1.1.0.jar
mysql-connector-java-8.0.11.jar
spring-beans-5.0.0.RELEASE.jar
spring-core-5.0.0.RELEASE.jar
spring-jdbc-5.0.0.RELEASE.jar
spring-tx-5.0.0.RELEASE.jar
路径:项目路径/web/WEB-INF/lib
导入后不要忘了:add as library
package glut.zongmin.domain;
/**
* 用户的JavaBean
*/
public class User {
private int id;
private String username;
private String password;
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package glut.zongmin.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;
/**
* JDBC工具类,使用Druid连接池技术
*/
public class JDBCUtils {
private static DataSource ds;
static {
try {
//1. 加载配置文件
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//2. 初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDs(){
return ds;
}
/**
* 获取Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
2). 创建glut.zongmin.dao.UserDao类,并在其中编写login方法
//声明一个JDBCTemplate对象来共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDs());
public User login(User loginUser){
try {
//1. 编写sql
String sql = "select * from user where username = ? and password = ?";
//2. 调用query方法
User user = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class),
loginUser.getUsername(),
loginUser.getPassword());
return user; //查询正常,则返回user对象
} catch (DataAccessException e) {
e.printStackTrace(); //暂时设置为输出控制台,今后会改为记录日志
return null; //出现异常,则返回user对象
}
}
@Test
public void tesLogin(){
User loginUser = new User();
loginUser.setUsername("superbay");
loginUser.setPassword("1234");
UserDao dao = new UserDao();
User user = dao.login(loginUser);
System.out.println(user);
}
3). 结果判断
package glut.zongmin.web.servlet;
import glut.zongmin.dao.UserDao;
import glut.zongmin.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("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 设置编码
req.setCharacterEncoding("UTF-8");
//2. 获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//3. 封装User对象
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);
//4. 调用UserDao的login方法
UserDao dao = new UserDao();
User user = dao.login(loginUser);
if(user == null){
//登录失败
req.getRequestDispatcher("/faithServlet").forward(req,resp);
}else {
//登录成功
//存储数据
req.setAttribute("user",user);
req.getRequestDispatcher("/successServlet").forward(req,resp);
}
}
}
2). FaithServlet类
package glut.zongmin.web.servlet;
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("/faithServlet")
public class FailthServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
resp.setContentType("text/html; charset=UTF-8");
//给页面写一句话
resp.getWriter().write("登录失败,用户名或密码错误!重新登录");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}
3). FaithServlet类
package glut.zongmin.web.servlet;
import glut.zongmin.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 {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
User user = (User)req.getAttribute("user");
if(user!=null){
resp.setContentType("text/html;charset=UTF-8");
//给页面写一句话
resp.getWriter().write("登录成功,"+""+user.getUsername()+""+"欢迎您!");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
}