用户访问登录页面,输入用户名和密码完成登录。如果正确则在页面中显示登录成功,如果失败则在页面中显示登录失败!
使用纯Servlet完成,包括页面的创建以及功能的实现。
创建t_user用户表
表名:T_USER | ||
---|---|---|
字段名 | 类型 | 约束 |
id | int | 主键 |
username | varchar(100) | |
password | varchar(20) |
1.创建Java Enterprise项目
2.在web目录下的WEB-INF目录中创建lib文件夹,导入数据库驱动包,并选中add as library
3.创建包结构
1. 在controller包中创建LoginPageServlet,继承HttpServlet
2. 重写service方法,在service方法中通过response对象的writer方法完成页面的输出
3. 在web.xml中配置该Servlet(使用注解配置更简洁)
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();
writer.write("");
writer.write("");
writer.write("");
writer.write("登录页面 ");
writer.write("");
writer.write("");
writer.write("");
writer.write("");
writer.write("");
}
}
LoginPageServlet
com.bjsxt.controller.LoginPageServlet
LoginPageServlet
/LoginPageServlet
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 interface UserDao {
User getUserByUsernameAndPassword(String username, String password);
}
public class UserDaoImpl implements UserDao {
@Override
public User getUserByUsernameAndPassword(String username, String password) {
User user = null;
Connection connection = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bjsxt01?serverTimezone=Asia/Shanghai";
connection = DriverManager.getConnection(url, "root", "root3306");
String sql = "select * from t_user 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 (Exception e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
}
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.getUserByUsernameAndPassword(username, password);
}
}
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码方式
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//接收参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//调用业务层处理请求
UserService userService = new UserServiceImpl();
User u = userService.login(username, password);
if(u == null){
resp.getWriter().write("登录失败!");
}else{
resp.getWriter().write("登录成功!");
}
}
}
LoginPageServlet
com.bjsxt.controller.LoginPageServlet
LoginPageServlet
/LoginPageServlet
LoginServlet
com.bjsxt.controller.LoginServlet
LoginServlet
/LoginServlet
1.用熟了配置以后就用注解配置
2.可以封装一个德鲁伊连接池的工具类
3.创建lib是在WEB-INF包里面,不然根本访问不了数据库和连接池(出现巨多问题)