文章目录
- 14.8 Request_登录案例
-
- 14.8.1 用户登录案例_需求
- 14.8.2 用户登录案例_分析
- 14.8.3 用户登录案例_代码实现
-
- 14.8.3.1 创建项目,导入html页面,导入配置文件,导入jar包(D:\CodeSoft\Install_File\lib)
- 14.8.3.2 创建数据库环境
- 14.8.3.3 创建包com.xww.domain,创建类User
- 14.8.3.4 创建包com.xww.util,编写工具类JDBCUtils
- 14.8.3.5 创建包com.xww.dao,创建类UserDao,提供login方法
- 14.8.3.6 编写包com.xww.web.servlet,创建LoginServlet类
- 14.8.3.7 编写FailServlet和SuccessServlet类
- 14.8.3.8 login.html中form表单的action路径的写法
- 14.8.3.9 BeanUtils工具类,简化数据封装
14.8 Request_登录案例
14.8.1 用户登录案例_需求
- 1.编写login.html登录页面 username & password 两个输入框;
- 2.使用Druid数据库连接池技术,操作mysql,day14数据库中user表 ;
- 3.使用JdbcTemplate技术封装JDBC ;
- 4.登录成功跳转到SuccessServlet展示:登录成功!用户名,欢迎您;
- 5.登录失败跳转到FailServlet展示:登录失败,用户名或密码错误。
14.8.2 用户登录案例_分析
14.8.3 用户登录案例_代码实现
14.8.3.1 创建项目,导入html页面,导入配置文件,导入jar包(D:\CodeSoft\Install_File\lib)
- commons-logging-1.2.jar
- druid-1.0.9.jar
- mchange-commons-java-0.2.12.jar
- mysql-connector-java-5.1.37-bin.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
14.8.3.2 创建数据库环境
CREATE DATABASE day14;
USE day14;
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32) UNIQUE NOT NULL,
PASSWORD VARCHAR(32) NOT NULL
);
14.8.3.3 创建包com.xww.domain,创建类User
package com.xww.domain;
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 + '\'' +
'}';
}
}
14.8.3.4 创建包com.xww.util,编写工具类JDBCUtils
package com.xww.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 {
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 getDataSource(){
return ds;
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
14.8.3.5 创建包com.xww.dao,创建类UserDao,提供login方法
package com.xww.dao;
import com.xww.domain.User;
import com.xww.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
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;
}
}
}
14.8.3.6 编写包com.xww.web.servlet,创建LoginServlet类
package cn.itcast.web.servlet;
import cn.itcast.dao.UserDao;
import cn.itcast.domain.User;
import org.apache.commons.beanutils.BeanUtils;
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;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
Map<String, String[]> map = req.getParameterMap();
User loginUser = new User();
try {
BeanUtils.populate(loginUser,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
UserDao dao = new UserDao();
User user = dao.login(loginUser);
if(user == null){
req.getRequestDispatcher("/failServlet").forward(req,resp);
}else{
req.setAttribute("user",user);
req.getRequestDispatcher("/successServlet").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
14.8.3.7 编写FailServlet和SuccessServlet类
package com.xww.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( "/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 {
this.doPost(request,response);
}
}
package com.xww.web.servlet;
import com.xww.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 {
User user = (User) request.getAttribute("user");
if(user != null){
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("登录成功!"+user.getUsername()+",欢迎您");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
14.8.3.8 login.html中form表单的action路径的写法
- 格式: 虚拟目录+Servlet的资源路径
- 示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/day14_test/loginServlet" method="post">
用户名:<input type="text" name="username"> <br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
14.8.3.9 BeanUtils工具类,简化数据封装
- BeanUtils工具类: 用于封装JavaBean。
- JavaBean: 标准的Java类。
- 要求:
- 类必须被public修饰 ;
- 必须提供空参的构造器 ;
- 成员变量必须使用private修饰 ;
- 提供公共setter和getter方法 。
- 功能: 封装数据
- 概念:
- 成员变量:
- 属性: setter和getter方法截取后的产物
- 例如: getUsername() --> Username–> username
- 方法:
setProperty()
getProperty()
populate(Object obj , Map map)
:将map集合的键值对信息,封装到对应的JavaBean对象中。