[JSP]쿠키, 데이터베이스
14장. 쿠키
1. 개요
2. 쿠키 생성
3. 쿠키 정보
4. 쿠키 삭제
1. 개요
쿠키: 클라이언트와 웹 서버 사이의 상태를 지속적으로 유지하는 방법. 상태 정보를 웹 서버가 아닌 클라이언트에 저장함
쿠키의 동작 과정
①쿠키 생성 단계: 주로 웹 서버가 생성. 응답 데이터에 함께 저장되어 웹 브라우저에 전송(response)
②쿠키 저장 단계: 웹 브라우저에서 응답 데이터에 포함된 쿠키를 쿠키 저장소에 보관
③쿠키 전송 단계: 요청이 있을 때마다 브라우저에서 서버로 전송
Cookie 클래스의 메서드
메서드 | 반환 유형 | |
getComment() | String | 쿠키 설명 반환 |
getDomain() | String | 쿠키의 유효한 도메인 정보 반환 |
getMaxAge() | int | 사용 가능 기간 정보 반환 |
getName() | String | 쿠키 이름 반환 |
getPath() | String | 유효한 디렉터리 정보 반환 |
getSecure() | boolean | 보안 설정 반환 |
getValue() | String | 설정된 값 반환 |
getVersion() | int | 쿠키 버전 반환 |
setComment(String) | void | 쿠키 설명 설정 |
setDomain(String) | void | 유효한 도메인 설정 |
setMaxAge(int) | void | 유효 기간 설정 |
setPath(String) | void | 유효한 디렉터리 설정 |
setSecure(boolean) | void | 쿠키 보안 설정 |
setValue(String) | void | 쿠키 값 설정 |
setVersion(int) | void | 쿠키 버전 설정 |
2. 쿠키 생성
response 내장 객체의 addCookie() 메서드 사용
Cookie Cookie(String name, String value)
<form action="cookie01_process.jsp" method="POST">
<p>아이디: <input type="text" name="id">
<p>비밀번호: <input type="text" name="passwd">
<p><input type="submit" value="전송">
</form>
<%
String user_id = request.getParameter("id");
String user_pw = request.getParameter("passwd");
if(user_id.equals("admin")&&user_pw.equals("1234")){
Cookie cookie_id = new Cookie("userID", user_id);
Cookie cookie_pw = new Cookie("userPW", user_pw);
response.addCookie(cookie_id);
response.addCookie(cookie_pw);
out.println("쿠키 생성에 성공했습니다<br>");
out.println(user_id + "님 환영합니다");
}else{
out.println("쿠키 생성에 실패했습니다");
}
%>
개발자 도구에서 쿠키를 확인할 수 있다.
3. 쿠키 정보
3.1 쿠키 객체 얻기
3.2 쿠키 객체 정보 얻기
3.1 쿠키 객체 얻기
쿠키 객체가 여러 개일 경우: 배열 형태로 가져옴
Cookie[] request.getCookies()
Cookie[] cookies = request.getCookies();
3.2 쿠키 객체의 정보 얻기
String getName()/String getValue()
Cookie[] cookies = request.getCookies();
for(int i = 0; i<cookies.length; i++){
out.println(cookies[i].getName() + ": "+cookies[i].getValue() + "<br>");
}
예제
<%
Cookie[] cookies = request.getCookies();
out.println("현재 설정된 쿠키의 개수: " + cookies.length + "<br>");
out.println("=======================<br>");
for(int i = 0; i<cookies.length; i++){
out.println("설정된 쿠키의 속성 이름 ["+i+"]: "+cookies[i].getName()+"<br>");
out.println("설정된 쿠키의 속성 값 ["+i+"]: " + cookies[i].getValue() + "<br>");
out.println("―――――――――――――――――――――<br>");
}
%>
4. 쿠키 삭제
쿠키의 기간을 만료시킴
void setMaxAge(int age) 사용
<%
Cookie cookie = new Cookie("memberId", "admin");
cookie.setMaxAge(0);
response.addCookie(cookie);
%>
예제
<%
Cookie[] cookies = request.getCookies();
for(int i = 0; i<cookies.length; i++){
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
response.sendRedirect("cookie02.jsp");
%>
15장. 데이터베이스 개발 환경 구축
1. 데이터베이스 개요
2. MySQL 개발 환경 구축
3. 통합 개발 환경과 데이터베이스 연동
4. MySQL 기본 명령어
1. 데이터베이스 개요
데이터베이스의 특성
- 통합된 자료: 똑같은 자료를 중복해서 저장하지 않음
- 컴퓨터가 액세스해서 처리할 수 있는 저장 장치에 수록된 자료
- 조직의 기능을 수행하는 데 없어서는 안 되는 자료
- 조직의 공동 자료. 응용 목적에 따라 사용자가 각자 다르게 사용 가능
DBMS: 데이터베이스 관리 시스템
데이터베이스를 관리하는 소프트웨어
MySQL
전세계적으로 가장 널리 사용되고 있는 오픈소스 관계형 데이터베이스 관리 시스템
SQL 사용
사용자 정의 가능: 오픈소스 GPL 라이선스를 통해 특정 환경에 맞게 소프트웨어 수정 가능
2. MySQL 설치
MySQL :: Download MySQL Installer
MySQL :: Download MySQL Installer
Note: MySQL 8.0 is the final series with MySQL Installer. As of MySQL 8.1, use a MySQL product's MSI or Zip archive for installation. MySQL Server 8.1 and higher also bundle MySQL Configurator, a tool that helps configure MySQL Server.
dev.mysql.com
MySQL 사이트에서 다운로드 후 실행
custom 설치에서 MySQL Server, Connector, MySQL Workbench를 선택
Check Requirements가 나오면 Execute를 눌러서 필요한 프로그램 설치
진행 후 Installation에서 Execute를 클릭해 설치
High Availability에서 Standalone 선택
Type and Networking은 수정하지 않고 진행
Authentication Method는 아래쪽 선택
Accounts and Roles에서 비밀번호를 설정하고 진행
설치 완료
4. MySQL 기본 명령어
SQL: 관계형 데이터베이스 관리 시스템에서 사용하는 언어
SQL 문장의 종류
1)데이터 정의 언어(DDL, Date Definition Language)
CREATE, ALTER, DROP 등
2)데이터 조작 언어(DML, Data Manipulation language)
SELECT, INSERT, DELETE, UPDATE 등
3)데이터 제어 언어(DCL, Data Control Language)
GRANT, REVOKE, COMMIT, ROLLBACK 등
데이터베이스 생성
CREATE DATABASE 데이터베이스명;
데이터베이스 사용
USE 데이터베이스명;
4.1 테이블 관련 명령어
1)테이블 생성
CREATE TABLE 테이블명(필드명1 자료형1[, 필드명2 자료형2, ....]);
create database JSPBookDB_test;
show databases;
use JSPBookDB_test;
-- use JSPBookDB_test;
-- create table JSPBookDB_test.sheet1 둘 중 하나 선택
create table JSPBookDB_test.sheet1(
id varchar(10),
password varchar(60),
age int,
name varchar(10)
);
실행 결과
2)테이블 조회
SHOW TABLES;
show tables; -- 사용하려면 useJSPBookDB_test 사용 필요
-- show JSPBookDB_test.tables ; 안 됨...
데이터베이스 내의 테이블 목록 조회 가능
3)테이블 세부 조회
DESC 테이블명;
desc JSPBookDB_test.sheet1;
4)테이블 구조 변경
ALTER TABLE 테이블명
①기존 테이블에 칼럼 추가
ALTER TABLE 테이블명 ADD 칼럼명 자료형
ALTER TABLE sheet1 ADD phone varchar(100);
②기존 테이블 칼럼 삭제
ALTER TABLE 테이블명 DROP COLUMN 칼럼명;
alter table sheet1 drop column phone;
③기존 테이블 칼럼 수정
ALTER TABLE 테이블명 CHANGE COLUMN 칼럼명 새칼럼명 자료형
alter table sheet1 change column password pw char(10);
5)테이블 이름 변경
RENAME TABLE 테이블명 TO 새테이블명[,
테이블명 TO 새테이블명, ...];
rename table sheet1 to sheet2;