1 首先導(dǎo)入jar包(看個(gè)人情況導(dǎo)入)
??
?這里需要注意的是平時(shí)我們導(dǎo)入jar包是導(dǎo)入在自己的工程或者moudle下面的,在這里我們必須把jar包導(dǎo)入在WEB-INF中(詳情可看圖),從圖中可看出,一個(gè)工程在編譯過(guò)后,只有src下的數(shù)據(jù)會(huì)編譯到WEB-INF下的classes目錄中,所以需要將靜態(tài)資源,jar包等資源放到web或者WEB-INF中。
???????????????????????????????????????????????????? 應(yīng)用與項(xiàng)目的關(guān)系
2 連接數(shù)據(jù)庫(kù),我們可以先導(dǎo)入配置文件utils(自己寫(xiě)的druid連接池)
public class DruidUtils {
private static DataSource ds;
static {
Properties pro = new Properties();
try {//1.加載配置文件
pro.load(DruidUtils.class.getResourceAsStream("jdbc.properties"));
//.2.獲取DataSource
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
throw new RuntimeException(e);
}
}//獲取連接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
//獲取連接池方法
public static DataSource getDataSource(){
return ds;
}
//釋放資源
public static void close(Connection conn, PreparedStatement stmt, ResultSet rs){
try {
if (rs == null) rs.close();
if (stmt == null) rs.close();
if (conn == null) rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
?編寫(xiě)配置文件(僅供參考,要注意的是末尾的.properties后綴名不能寫(xiě)錯(cuò))
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///wyb?serverTimezone=GMT
username=root
password=gg791480
3.編寫(xiě)登錄代碼:
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "select * from user where name=? and poaaword=?";
try {
User user = template.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),username,password);
response.sendRedirect(request.getContextPath()+"index.jsp");
}catch (Exception e){
response.sendRedirect(request.getContextPath()+"/html/loser.html");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.接下來(lái)我們用Java的三層架構(gòu)規(guī)范來(lái)實(shí)現(xiàn)功能
4.1 首先用三成架構(gòu)的方式分析登錄功能,如果所示,我們?cè)趙eb包下實(shí)現(xiàn)獲取參數(shù),發(fā)送請(qǐng)求,service包下處理數(shù)據(jù),dao包下連接數(shù)據(jù)庫(kù)
4.2如圖,我們?cè)趕rc下創(chuàng)建一個(gè)包,然后再包下再創(chuàng)建如下包
?web層下代碼:
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private UserSerivce userSerivce =new UserSerivceImpl();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
User loginUser = userSerivce.login(username, password);
request.getSession().setAttribute("user",loginUser);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}catch (Exception e){
request.setAttribute("error",e.getMessage());
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
test層(測(cè)試代碼用)
utils層(編寫(xiě)配置文件,與上述一致)
service層代碼(一般會(huì)先寫(xiě)一個(gè)接口,然后再實(shí)現(xiàn)其接口
public interface UserSerivce {
User login(String username, String password) throws Exception;
}
)
public class UserSerivceImpl implements UserSerivce {
private UserDao userDao = new UserDaoImpl();
@Override
public User login(String username,String password) throws Exception {
User user = userDao.findByUsernameAndPassword(username, password);
if (user == null) {
throw new Exception("登錄或密碼錯(cuò)誤");
}
return user;
}
}
domain成代碼(通過(guò)是寫(xiě)實(shí)體類(lèi))
public class User {
private Integer id;
private String name;
private Integer password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPassword() {
return password;
}
public void setPassword(Integer password) {
this.password = password;
}
}
dao層(連接數(shù)據(jù)庫(kù)代碼)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-775758.html
public interface UserDao {
User findByUsernameAndPassword(String username, String password);
}
public class UserDaoImpl implements UserDao {
private JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
@Override
public User findByUsernameAndPassword(String username,String password){
String sql = "select * from user where name=? and possword=?";
List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), username, password);
return list.size() == 0 ? null : list.get(0);
}
}
4.3 登錄頁(yè)面(login.jsp,參考)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-775758.html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login" method="post">
用戶(hù)名<input type="text" name="username"><br>
密碼<input type="password" name="password"><br>
<sp>${error}</sp>
<input type="submit" value="登錄">
</form>
</body>
</html>
到了這里,關(guān)于JavaEE 課堂案例: 簡(jiǎn)單實(shí)現(xiàn)登錄功能: 1.前端用戶(hù)自己輸入賬號(hào)密碼, 點(diǎn)擊登錄 2.服務(wù)器端獲得賬號(hào)密碼, 數(shù)據(jù)庫(kù)查詢(xún) jar JdbcTemplate 3.登錄成功 -> 跳轉(zhuǎn)到首頁(yè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!