티스토리 뷰
https://youtube.com/playlist?list=PLRx0vPvlEmdAdWCQeUPnyMZ4PsW3dGuFB
JSP 강의평가 웹 사이트 개발하기 (JSP Lecture Evaluation Service Development Tutorial)
www.youtube.com
동빈나님의 JSP 강의를 진행하며 일부 과정을 요약한 내용입니다. 강의와 블로그 작성 코드가 조금씩 다를 수 있습니다.
개발환경
- 브라우저 : 크롬
- 언어 : 자바
- IDE : 이클립스 EE
- DB : MySQL 8.0.26(다운로드) / mysql-connector-java-8.0.26(다운로드)
- 웹 컨테이너 : 톰캣 8.5v
프로젝트 생성
프로젝트 구조
Java
- UserDTO.java : 데이터베이스 모델
- UserDAO.java : 데이터베이스 연산
- DatabaseUtil.java : DB 연동
Jsp
- Index : userID와 userPassword를 전달
- userJoinAction : ID와 Password가 정상적으로 전달 되었다면, UserDAO의 join() 메서드를 활용해 DB반영
user/UserDTO.java
package user;
public class UserDTO {
String userID;
String userPassword;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
user/UserDAO.java
package user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import util.DatabaseUtil;
// 실질적으로 데이터베이스 연산(Access)을 담당
public class UserDAO {
public int join(String userID, String userPassword) {
String SQL = "INSERT INTO USER VALUES (?, ?)"; // ID, Pass
try {
Connection conn = DatabaseUtil.getConnection(); // static 선언
PreparedStatement pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, userID);
pstmt.setString(2, userPassword);
return pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}
return -1;
}
}
util/DatabaseUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseUtil {
// TUTORIAL 데이터베이스와 연동하는 과정
public static Connection getConnection() {
try {
String dbURL = "jdbc:mysql://localhost:3306/TUTORIAL";
String dbID = "root";
String dbPassword = "1234";
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(dbURL,dbID, dbPassword);
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
mysql-Connector 버전을 현재버전 8.0.26 으로 사용하기 위해선 Class 참초 경로가 강의와 달라야한다. 강의에서 사용하는 버전과 동일한 버전의 mysql-Connector 를 사용한다면 .cj를 생략해도된다.
index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Hello World</title>
</head>
<body>
<form action="./userJoinAction.jsp" method="post">
<input type="text" name="userID">
<input type="password" name="userPassword">
<input type="submit" value="회원가입">
</form>
</body>
</html>
userJoinAction.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- import in jsp -->
<%@ page import="user.UserDTO" %>
<%@ page import="user.UserDAO" %>
<%@ page import="java.io.PrintWriter" %> <!-- js 출력 -->
<%
request.setCharacterEncoding("UTF-8");
String userID = null;
String userPassword = null;
if(request.getParameter("userID") != null){
userID = (String) request.getParameter("userID");
}
if(request.getParameter("userPassword") != null){
userPassword = (String) request.getParameter("userPassword");
}
if(userID == null || userPassword == null){
/* PrintWriter: 사용자에게 구문 출력하기 위함 */
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('아이디와 비밀번호를 확인해주세요.')");
script.println("history.back();"); /* 이전 페이지로 이동 */
script.println("</script>");
script.close();
return;
}
/* 회원가입이 정상적으로 이루어졌다면 */
UserDAO userDAO = new UserDAO();
int result = userDAO.join(userID, userPassword);
if(result == 1){
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('회원가입이 되었습니다.')");
script.println("location.href = 'index.jsp';"); /* index.jsp로 이동 */
script.println("</script>");
script.close();
return;
}
%>
user 테이블
mysql> SELECT * FROM user;
+--------+--------------+
| userID | userPassword |
+--------+--------------+
| 1 | 123 |
| user | 1234 |
| hello | 1234 |
+--------+--------------+
3 rows in set (0.00 sec)
'Computer Science > Etc' 카테고리의 다른 글
무료 온라인 코딩 사이트 추천 (0) | 2022.06.16 |
---|---|
REST API와 RESTful API란? (0) | 2022.04.06 |
2020 공공빅데이터 청년인턴십 후기 (0) | 2021.09.10 |
한국품질재단 클라우드 교육과정 후기 (0) | 2021.09.06 |
[JSP] 회원가입 및 이메일 인증 구현하기 (0) | 2021.08.31 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- svn
- SQL
- 환경설정
- CSS
- CS
- 부트스트랩
- 데이터베이스
- 넥사크로
- 백준
- HeidiSQL
- Open API
- 스프링부트
- C
- C++
- 인턴
- JVM
- 오라클
- 프로그래머스
- 네트워크
- JSP
- 개발용어
- 오류
- 국비교육
- 스프링
- Thymeleaf
- 이클립스
- Java
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함