用户登录案例需求
- 编写login.html登录页面
username&password两个输入框
- 使用Druid数据库连接池技术,操作MySQL,duing数据库中的user表
- 使用JdbcTemplate技术封装JDBC
- 登陆成功跳转到SuccessServlet展示:登陆成功!用户名,欢迎您
- 登陆失败跳转到FailServlet展示:登录失败!用户名或密码错误
登录案例逻辑分析
开发步骤
- 创建项目,导入html页面,配置文件,jar包
- 创建数据库的环境
create database duing;
use duing;
create table user(
id int primary key auto_increment,
username varchar(32) unique not null,
password varchar(32) not null
);
3. 创建包duing.domain,创建User类
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 + '\'' +
'}';
}
}
- 创建包ding.util,编写工具类JDBCUtils
public class JDBCUtils {
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 (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSouce(){
return ds;
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
- 创建包duing.dao,创建类UserDao,提供login方法
public class UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSouce());
public User login(User loginUser){
try{
String sql = "select * from user where username = ? and password = ?";
User user = template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),loginUser.getUsername(),loginUser.getPassword());
return user;
}catch (DataAccessException e){
e.printStackTrace();
return null;
}
}
}
- 单元测试,测试数据库登录链接是否可行
public class UserDaoTest {
@Test
public void testLogin(){
User loginuser = new User();
loginuser.setUsername("zhangsan");
loginuser.setPassword("123");
UserDao dao = new UserDao();
User user = dao.login(loginuser);
System.out.println(user);
}
}
- 测试结果:
- 编写duing.web.servlet.LoginServlet类
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);
UserDao dao = new UserDao();
User user = dao.login(loginUser);
if(null==user){
request.getRequestDispatcher("/failServlet").forward(request,response);
}else{
request.setAttribute("user",user);
request.getRequestDispatcher("/successServlet").forward(request,response);
}
}
}
@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("登录失败!用户名或密码错误");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User user = (User) request.getAttribute("user");
if(null != user){
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登录成功!"+user.getUsername()+",欢迎您");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
- login.html中form表单的action路径的写法