此次连接数据库,将登录页面的数据和数据库的数据进行比较,登录返回结果。
jdk
Tomcat
1.创建登录页面
* 创建Servlet进行登录页面请求处理
2.点击登录完成登录操作
* 浏览器发送请求到服务器(用户信息+其他数据)
* 服务器调用对应的servlet进行处理
* 设置响应编码格式
* 获取请求信息
*处理请求信息
*响应处理结果
3.在servlet中完成用户登录校验
*需要连接数据库(创建用户表)
使用MVC思想完成
USE db1;
## 创建用户表
CREATE TABLE t_user(
uid INT(10) NOT NULL AUTO_INCREMENT,
unname VARCHAR(100) NOT NULL,
pwd VARCHAR(100) NOT NULL,
PRIMARY KEY(uid)
)
## 添加测试数据
INSERT INTO t_user VALUES(DEFAULT,'zhangsan','123');
INSERT INTO t_user VALUES(DEFAULT,'lisi'.'456');
SELECT * FROM t_user;
(1)在src下创建servlet包,创建一个LoginServlet类
package 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("/ck")
public class PageServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应编码格式
resp.setContentType("text/html; charset=utf-8");
//获取请求信息
//处理请求
//响应处理结果
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write(");
resp.getWriter().write("用户名:
");
resp.getWriter().write("密码:
");
resp.getWriter().write("
");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
}
}
(2)form表单跳转请求页面,在servlet包创建一个PageSevlet类
package servlet;
import pojo.User;
import service.LoginService;
import service.impl.LoginServicImpl;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
//设置编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String uname = req.getParameter("uname");
String pwd = req.getParameter("pwd");
System.out.println(uname + ":" + pwd);
//处理请求信息
//获取业务层对象
LoginService ls = new LoginServicImpl();
User u = ls.checkLoginService(uname, pwd);
System.out.println(u);
//响应处理结果
if(u!=null){
resp.getWriter().write("登录成功");
}else{
resp.getWriter().write("登录失败");
}
}
}
(1)创建LoginService接口
package service;
import pojo.User;
public interface LoginService {
User checkLoginService(String uname, String pwd);
}
(2)创建LoginServicImpl对象,在service包下创建impl包
package service.impl;
import dao.Impl.LoginDaoImpl;
import dao.LoginDao;
import pojo.User;
import service.LoginService;
public class LoginServicImpl implements LoginService {
//创建Dao层过度对象
LoginDao ld = new LoginDaoImpl();
@Override
public User checkLoginService(String uname, String pwd) {
return ld.checkLoginDao(uname, pwd);
}
}
package pojo;
import java.util.Objects;
public class User {
private int uid;
private String uname;
private String pwd;
public User() {
}
public User(int uid, String uname, String pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return uid == user.uid &&
Objects.equals(uname, user.uname) &&
Objects.equals(pwd, user.pwd);
}
@Override
public int hashCode() {
return Objects.hash(uid, uname, pwd);
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
(1)编辑LoginDao接口
package dao;
import pojo.User;
public interface LoginDao {
User checkLoginDao(String uname, String pwd);
}
(2)在dao包下创建Impl包,编写LoginDaoImpl代码继承LoginDao接口
package dao.Impl;
import dao.LoginDao;
import pojo.User;
import java.sql.*;
public class LoginDaoImpl implements LoginDao {
@Override
public User checkLoginDao(String uname, String pwd) {
//声明JDBC对象
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//声明数据存储对象
User u = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
conn = DriverManager.getConnection("jdbc:mysql://cdb-4zpu53j4.cd.tencentcdb.com:10183/db1", "root", "puge1234");
//创建Sql命令
String sql = "select * from t_user where uname=? and pwd=?";
//创建sql命令对象
ps = conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, uname);
ps.setString(2, pwd);
//执行
rs = ps.executeQuery();
//遍历执行结果
while (rs.next()) {
u = new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
}
//关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//返回
return u;
}
}