使用者登入案例【13】
阿新 • • 發佈:2018-12-11
使用者登入
使用者登入案例需求:
1.編寫login.html登入頁面
username & password 兩個輸入框
2.使用Druid資料庫連線池技術,操作mysql,day14資料庫中user表
3.使用JdbcTemplate技術封裝JDBC
4.登入成功跳轉到SuccessServlet展示:登入成功!使用者名稱,歡迎您
5.登入失敗跳轉到FailServlet展示:登入失敗,使用者名稱或密碼錯誤
開發步驟
1. 建立專案,匯入html頁面,配置檔案,jar包
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 );
3. 建立包cn.itcast.domain,建立類User
package cn.itcast.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 + '\'' + '}'; } }
4. 建立包cn.itcast.util,編寫工具類JDBCUtils
package cn.itcast.util; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import javax.xml.crypto.Data; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; /** * JDBC工具類 使用Durid連線池 */ public class JDBCUtils { private static DataSource ds ; static { try { //1.載入配置檔案 Properties pro = new Properties(); //使用ClassLoader載入配置檔案,獲取位元組輸入流 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 getDataSource(){ return ds; } /** * 獲取連線Connection物件 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } }
5. 建立包cn.itcast.dao,建立類UserDao,提供login方法
package cn.itcast.dao;
import cn.itcast.domain.User;
import cn.itcast.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* 操作資料庫中User表的類
*/
public class UserDao {
//宣告JDBCTemplate物件共用
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* 登入方法
* @param loginUser 只有使用者名稱和密碼
* @return user包含使用者全部資料,沒有查詢到,返回null
*/
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;
} catch (DataAccessException e) {
e.printStackTrace();//記錄日誌
return null;
}
}
}
6. 編寫cn.itcast.web.servlet.LoginServlet類
package cn.itcast.web.servlet;
import cn.itcast.dao.UserDao;
import cn.itcast.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 {
//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);
//5.判斷user
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);
}
}
7. 編寫FailServlet和SuccessServlet類
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取request域中共享的user物件
User user = (User) request.getAttribute("user");
if(user != null){
//給頁面寫一句話
//設定編碼
response.setContentType("text/html;charset=utf-8");
//輸出
response.getWriter().write("登入成功!"+user.getUsername()+",歡迎您");
}
}
@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);
}
}
8. login.html中form表單的action路徑的寫法
* 虛擬目錄+Servlet的資源路徑
9. BeanUtils工具類,簡化資料封裝
* 用於封裝JavaBean的
1. JavaBean:標準的Java類
1. 要求:
1. 類必須被public修飾
2. 必須提供空參的構造器
3. 成員變數必須使用private修飾
4. 提供公共setter和getter方法
2. 功能:封裝資料
2. 概念:
成員變數:
屬性:setter和getter方法擷取後的產物
例如:getUsername() --> Username--> username
3. 方法:
1. setProperty()
2. getProperty()
3. populate(Object obj , Map map):將map集合的鍵值對資訊,封裝到對應的JavaBean物件中