login.java ํ์ผ ์์ฑ

MySQL ํ ์ด๋ธ ์์ฑ


test
LoginDAO dao = new LoginDAOImple();
Login login = new Login();
login.setEmail("dm1234dms@gmail.com");
login.setPassword("1234");
System.out.println(dao.loginCheck(login));

LoginDAO(์ธํฐํ์ด์ค) ํ์ผ ๋ฐ ์ธํฐํ์ด์ค ๊ตฌํํ LoginDAOImple ํ์ผ ์์ฑ

LoginDAO
package webapp.dao;
import webapp.model.Login;
public interface LoginDAO {
//๋ก๊ทธ์ธ ์ฒดํฌ
String loginCheck(Login loginbean);
}
LoginDAOImple
package webapp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import webapp.model.Login;
import webapp.util.DBConn;
public class LoginDAOImple implements LoginDAO {
//add
@Override
public String loginCheck(Login loginbean) {
String sql = "SELECT * FROM login WHERE email=? and password=?";
try {
Connection conn = DBConn.getConnection();// db ์ฐ๊ฒฐ
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, loginbean.getEmail());
ps.setString(2, loginbean.getPassword());
ResultSet rs = ps.executeQuery();
// ์ด๋ฉ์ผ๊ณผ ํจ์ค์๋๊ฐ ์์ผ๋ฉด "true" ์์ผ๋ฉด "false"๋ฆฌํด
if (rs.next()) {
return "true";
} else {
return "false";
}
} catch (Exception e) {
e.printStackTrace();
}
return "error";// catch๋ฌธ์์ ๋์ค๋ฉด "error"๋ก ๋ฆฌํด
}
}

LoginController
package webapp.controller;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
import webapp.dao.LoginDAO;
import webapp.dao.LoginDAOImple;
import webapp.model.Login;
//์ฃผ์๋ณ๊ฒฝ
@WebServlet("/loginprocess")
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;
LoginDAO loginDAO = null;
public LoginController() {
loginDAO = new LoginDAOImple(); // ์ฒ์๋ง ๊ฐ์ฒด ์์ฑํ๋ฉด๋๋ฏ๋ก
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Login login = new Login();
login.setEmail(request.getParameter("email"));
login.setPassword(request.getParameter("password"));
String result = loginDAO.loginCheck(login);
if(result.equals("true")) {
session.setAttribute("email", login.getEmail());
response.sendRedirect("EmployeeController?action=LIST");
}
if(result.equals("false")) {
response.sendRedirect("index.jsp?status=false");
}
if(request.equals("error")) {
response.sendRedirect("index.jsp?status=error");
}
}
}
index.jsp
<html>
<head>
<title>Login System</title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<body>
<%
String email=(String)session.getAttribute("email");
//redirect user to home page if already logged in
if(email!=null){
response.sendRedirect("EmployeeController?action=LIST");
}
String status=request.getParameter("status");
if(status!=null){
if(status.equals("false")){
out.print("Incorrect login details!");
}
else{
out.print("Some error occurred!");
}
}
%>
<div class="container">
<form action="loginprocess" method="post">
<div class="card">
<div class="card-header text-left font-weight-bold">
Login
</div>
<div class="card-body">
<div class="form-group">
<input type="text" name="email" required class="form-control" placeholder="Enter Email"/>
</div>
<div class="form-group">
<input type="password" name="password" required class="form-control" placeholder="Enter password"/>
</div>
</div>
<div class="card-footer text-left">
<input type="submit" value="Login" class="btn btn-primary"/>
</div>
</div>
</form>
</div>
</body>
</html>

logout.jsp
<%
session.invalidate();
response.sendRedirect("index.jsp");
%>
xml ๊ธฐ๋ณธ ํ์ด์ง ์์
-> index.jsp๋ก
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>DATABASE</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- ํ๊ธ์ค์ ํํฐ์ถ๊ฐ -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ํ๊ธ์ค์ END -->
</web-app>
.
employee-list.jsp ์์
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>Employee Directory</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
//๋ก๊ทธ์์ ์คํ์ผ ์ถ๊ฐ
<style>
.logout{
display: flex;
justify-content: space-between;
}
</style>
</head>
<body>
//์ถ๊ฐ๋๋ ๋ถ๋ถ
<%
String email = (String) session.getAttribute("email");
if (email == null) {
response.sendRedirect("index.jsp");
}
%>
<div class="container">
<h1>Employee Directory</h1>
<hr />
<p>${NOTIFICATION}</p>
<p class="logout">
<button class="btn btn-primary"
onclick="window.location.href = 'views/employee-form.jsp'">์ง์
์ถ๊ฐ</button>
<a href="logout.jsp">๋ก๊ทธ์์</a> //๋ก๊ทธ์์ ๋ฒํผ ์ถ๊ฐ
</p>
<table class="table table-striped table-bordered">
<tr class="thead-dark">
<th>Name</th>
<th>Department</th>
<th>Date of birth</th>
<th>Actions</th>
</tr>
<c:forEach items="${list}" var="employee">
<tr>
<td>${employee.name}</td>
<td>${employee.department}</td>
<td>${employee.dob}</td>
<td><a
href="${pageContext.request.contextPath}/EmployeeController?action=EDIT&id=${employee.id}">Edit</a>
| <a onclick="return confirm('์ญ์ ํ๊ฒ ์ต๋๊น?')"
href="${pageContext.request.contextPath}/EmployeeController?action=DELETE&id=${employee.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>

๋ก๊ทธ์์ ๋ฒํผ ๋๋ฅผ ์

๋ฐ์ดํฐ ํ ์ด๋ธ
๋ง์ฝ ๋ฐ์ดํฐ๊ฐ ๋ง์ด ์์ด๊ฒ๋๋ฉด ์๋๋ก ๊ธธ์ด์ง๊ฒ๋๋ค.

๊ทธ๋์
ํํ์ด์ง์ 5๊ฐ์ฉ๋ง ๋ค์ด๊ฐ ์ ์๋๋ก ๋ฐ์ดํฐ ํ ์ด๋ธ์ ๋ง๋ค์ด์ค๋ค.
https://datatables.net/manual/installation
Installation
Installation DataTables is a powerful Javascript library for adding interaction features to HTML tables, and while simplicity is a core design principle for the project as a whole, it can appear quite daunting to get started. However, taking those first st
datatables.net
jQuery CDN
jQuery CDN – Latest Stable Versions jQuery Core Showing the latest stable release in each major branch. See all versions of jQuery Core. jQuery 3.x jQuery 2.x jQuery 1.x jQuery Migrate jQuery UI Showing the latest stable release for the current and legac
releases.jquery.com
employee-list.jsp ์์
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>Employee Directory</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<!-- ๋ฐ์ดํฐ ํ
์ด๋ธ -->
<link rel="stylesheet"
href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.css" />
<style>
.logout {
display: flex;
justify-content: space-between;
}
</style>
</head>
<body>
<%
String email = (String) session.getAttribute("email");
if (email == null) {
response.sendRedirect("index.jsp");
}
%>
<div class="container">
<h1>Employee Directory</h1>
<hr />
<p>${NOTIFICATION}</p>
<p class="logout">
<button class="btn btn-primary"
onclick="window.location.href = 'views/employee-form.jsp'">์ง์
์ถ๊ฐ</button>
<a href="logout.jsp">๋ก๊ทธ์์</a>
</p>
<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr class="thead-dark">
<th>Name</th>
<th>Department</th>
<th>Date of birth</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="employee">
<tr>
<td>${employee.name}</td>
<td>${employee.department}</td>
<td>${employee.dob}</td>
<td><a class="btn btn-info"
href="${pageContext.request.contextPath}/EmployeeController?action=EDIT&id=${employee.id}">Edit</a>
| <a class="btn btn-danger" onclick="return confirm('์ญ์ ํ๊ฒ ์ต๋๊น?')"
href="${pageContext.request.contextPath}/EmployeeController?action=DELETE&id=${employee.id}">Delete</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<!-- ์คํฌ๋ฆฝํธ ์ถ๊ฐ -->
<!-- jQuery CDN -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function() {
$("#datatable").DataTable({
'language' : {
'lengthMenu' : 'ํ์ํ ์ค์ ์ ํ _MENU_',
'search' : '๊ฒ์',
'paginate' : {
'previous' : '์ด์ ',
'next' : '๋ค์'
},
"info" : "ํ์ด์ง _PAGE_ / _PAGES_",
'infoEmpty' : '๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.',
"infoFiltered" : "(์ ์ฒด ํ์ด์ง _MAX_ ์์ ๊ฒ์)",
'thousands' : ','
},
'lengthMenu' : [ 5, 10, 25 ],
'pageLength' : 5,
'ordering' : false,
'stateSave' : true
});
})
</script>
</body>
</html>

'DB > MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ฐ์ดํฐ๋ฒ ์ด์ค (์คํค๋ง) ํ ํ์ผ๋ก ๋ฌถ์ด ๋ด๋ณด๋ด๊ธฐ export (0) | 2023.09.27 |
|---|---|
| Simple DB ํ๋ก์ ํธ-2 (0) | 2023.09.21 |
| Simple DB ํ๋ก์ ํธ (0) | 2023.09.21 |