티스토리 뷰

학교공부

기말고사

날따라해봐요요롷게 2021. 6. 14. 15:48

13 - 세션

httpSession 인터페이스

 

# 세션을 생성하는 방법

- session.setAttribute(String name, Object value);

- HttpSession  httpSession = request.getSession();

- <%@ page session = "true" %>

 

# 세션정보 얻기 (반환유형 조심!)

- getAttribute() - 반환유형

- getAttributeNames() - 반환유형

 

# 세션 삭제

- removeAttribute()

- invalidate()

 

# 세션 유효 시간 설정

- setMaxInactiveInterval(60*60)

- getLastAccessedTime()

- getCreationTime() 

 

13 - 연습문제

1. 세션이란 무엇인가

 : 클라이언트와 웹 서버 간의 상태를 지속적으로 유지하는 방법

 

2. JSP 페이지에서 세션을 설정, 삭제 하는 메소드는 무엇인가?

 설정 : setAttribute(String name, Object value)

- HttpSession httpSession  = request.getSession()

- <%@ page session = "true" %>

 

삭제 : removeAttribute(String name) ==> name 은 세션 속성 이름

invalidate() ==> 세션 전체 삭제

 

3. 세션 정보를 얻어오는 메소드

- Object getAttribute(String name) ==> 반환유형에 맞는 형변환이 일어나야한다.

- Enumeration getAttributeNames(String name) ==> 다중 세션의 정보를 enum의 형으로 받아옴

 

4. 

	 // sesion.jsp
     <form action="session_process.jsp" method="post">
	 	<p> 아이디 : <input type="text" name="id">
	 	<p> 비밀번호 : <input type="text" name="passwd">
	 	<input type="submit" value="전송">
	 </form>
     
     //session_process.jsp
     	 <%
	 		String id = request.getParameter("id");
	 		String passwd = request.getParameter("passwd");
	 		
	 		
	 		if(id.equals("admin") && passwd.equals("1234")){
	 			session.setAttribute("userID", id);
	 			session.setAttribute("userPwd", passwd);
	 			out.println("세션 생성");
	 			response.sendRedirect("welcome.jsp");
	 		}
	 		
	 %>
     
     // welcome.jsp
     	<%
		String user_id = (String)session.getAttribute("userID");
	
		if(user_id == null){
			response.sendRedirect("session_out.jsp");
		}
	%>
	<h3><%=user_id%> 님 반갑습니다.</h3>
	<p> <a href="session_out.jsp">로그아웃</a>
    
    // session_out.jsp
    	<%
		session.invalidate();
		response.sendRedirect("session.jsp");
	%>

 

14 - 쿠키

# 쿠키

 : 세션과 같이 클라이언트와 웹 서버 간의 상태를 지속적으로 유지하는 방법이다.

단, 차이점은 상태 정보를 웹 서버가 아닌 클라이어언트에 저장한다.

 

# 동작과정

클라이언트가 서버에 요청을 할 때 쿠키정보를 같이 보낸다.

서버는 요청안에 포함된 쿠키정보를 읽어 처리한다.

 

- 쿠키 생성 : 웹 서버가 쿠키 생성 후 응답 데이터와 함께 웹 브라우저에 전송

- 쿠키 저장 : 응답 데이터에 포함된 쿠키를 쿠키 저장소에 보관한다.

- 쿠키 전송 : 한 번 저장된 쿠키를 요청이 있을 때마다 웹 서버에 쿠키를 전송

 

JSP 페이지는 쿠키를 사용하기 위해서 Cookie 클래스를 사용한다.

 

# 쿠키 생성

: Cookie cookie = new Cookie(String name, String value)

response.addCookie(cookie); ==> 쿠키 객체 설정

 

# 쿠키 정보

: request 내장 객체 getCookie() 메소드로 쿠키의 객체 를 얻어온다.

얻어온 객체는 getName(), getValue() 메소드를 사용하여 쿠키의 이름과 값을 얻어온다.

 

- 객체 얻기 : Cookie[] cookies = request.getCookies();

- 객체 정보 얻기 : 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>");
}

# 쿠키 삭제

: 쿠키를 삭제하는 메소드는 없음 유효시간 설정을 통하여 삭제를 한다.

setMaxAge() 유효기간을 0으로 설정한다.

Cookie cookie = new Cookie("user_id", "admin");
cookie.setMaxAge(0);
response.addCookie(cookie);

14 - 연습문제 

1. JSP 페이지에 쿠키를 설정하는 메소드, 설정된 쿠키 정보를 얻어오는 메소드는 무엇인가?

 

설정 : 쿠키 객체 생성 -> 객체 설정

Cookie cookie = new Cookie("user_id", "admin");

response.addCookie(cookie);

 

정보 얻어오기 : 객체 얻어오기 -> 객체를 활용하여 value 얻어오기

Cookie cookie = request.getCookie();

name = cookie.getName();

value = cookie.getValue();

 

2. 쿠키 삭제하는 방법

: 유효기간을 0으로 설정하여 객체를 설정한다. setMaxAge(0)

Cookie cookie = new Cookie("user_id", "admin");

cookie.setMaxAge(0);

response.addCookie(cookie);

 

3. 

	// cookie.jsp
    <form action="cookie_process.jsp" method="post">
		ID : <input type="text" name="id">
		PASSWORD : <input type="text" name ="pwd">
		<input type="submit" value="전송" > 
	</form>
     // cookie_process.jsp
     	<%
		String user_id = request.getParameter("id");
		String user_pwd = request.getParameter("pwd");
		
		if(user_id.equals("admin") && user_pwd.equals("1234")){
			Cookie cookie_id = new Cookie("id", user_id);
			Cookie cookie_pwd = new Cookie("pwd", user_pwd);
			
			response.addCookie(cookie_id);
			response.addCookie(cookie_pwd);
			out.println("쿠키 추가");
			response.sendRedirect("welcome.jsp");
		} else {
			out.println("아이디, 비밀번호가 일치하지 않음!");
		}
	%>
    // welcome.jsp
    	<%
		Cookie[] cookie = request.getCookies();

		if(cookie[0] == null) response.sendRedirect("cookie_out.jsp");
	%>
	
	<h3><%=cookie[0].getValue()%> 님 반갑습니다.</h3>
	<a href="cookie_out.jsp">로그아웃</a>
    // cookie_out.jsp
	<%
		Cookie[] cookie = request.getCookies();
		
		for(int i = 0; i<cookie.length; i++){
			cookie[i].setMaxAge(0);
			response.addCookie(cookie[i]);
		}
		out.println("쿠키 삭제!!");
		response.sendRedirect("cookie.jsp");
	%>

 

15. MySQL

 

DDL : CREATE, ALTER, DROP

 

CREATE TABLE student(
	id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(10),
    dapart VARCHAR(5);
)

ALTER TABLE student add class INTEGER; // 필드 추가
ALTER TABLE student drop name;	// 컬럼 삭제
ALTER TABLE student change depart Department VARCHAR(11);	// 컬럼 명,타입 바꾸기
ALTER TABLE student rename studentInfomation;	// 테이블 이름 바꾸기

 

 

DML : INSERT, SELECT, DELETE, UPDATE

- SELECT (검색)

SELECT 필드 필드별명 FROM 테이블

SELECT name as 이름 FROM student;

SELECT CONCAT(name-id) as 이름-번호 FROM student;

SELECT DISTINCT depart from student; ==> 학과를 출력하되 중복은 제거한다.

 

- ORDER BY(정렬)

SELECT name, id, depart FROM student ORDER BY name;

SELECT name as 이름, id as 학번, depart as 학과 FROM student ORDER BY 이름;

SELECT name as 이름, id as 학번, depart as 학과 FROM student ORDER BY 학번 DESC;

 

- WHERE (조건절)

SELECT name, id FROM student WHERE name = "lee";

SELECT id, name FROM student WHERE id BETWEEN 10000 and 20000;

SELECT id, name, depart FROM student WHERE depart in("com", "lit");

SELECT name, id, depart FROM student WHERE name like '%L%'; ==> 이름에서 L이 들어가는 데이터를 모두 검색

 

-INSERT (데이터 삽입)

INSERT INTO student (name, id) values("LEE", "20001");

 

-UPDATE(수정)

UPDATE student SET name="KIM" WHERE id="20001";

 

DELETE(삭제)

DELETE FROM student WHERE name = "LEE";

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'학교공부' 카테고리의 다른 글

(학교공부) JAVA - 스레드  (0) 2021.11.02
chapter - 15  (0) 2021.07.02
JSP - 1  (0) 2021.03.03
구조체, 공용체 - #1  (0) 2021.02.16
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
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
29 30
글 보관함