글로리아-89 2024. 4. 16. 14:37
728x90

<Ex03_makeSession.java>

package session;

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;


@WebServlet("/Ex03_makeSession")
public class Ex03_makeSession extends HttpServlet {

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		// Session: 클라이언트 정보를 유지하기 위한 방법
		//1. 저장위치: 서버 pc
		//2. 보안 : 강력
		//3. 자원: 서버의 자원을 사용하기 때문에 마구사용 안됨 (비용이 발생되는 이유로)
		//4. 용량: 서버가 허용하는 한  제한은 없다. 
		//5. 저장형식: Object 형태로 저장>> 모든 객체를 담을 수 있음
		
		//1. Session 객체 생성
		HttpSession session = request.getSession();
		
		//2. Session 생성 session.setAttribute("이름", "값");
		session.setAttribute("이름", "yyj");
		session.setAttribute("classA", "데이터비즈니스");
		
		//3. Session 유효기간 설정 (초 단위)
		session.setMaxInactiveInterval(60*60);
		
		response.getWriter().print("<h1>MakeSession</h1>");	
	}
}

 

<Ex04_showSession.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	//4. Session 조회 session.getAttribute("이름")>>> object >> 에러 > 형변화 필요
	// session >> 내장객체로 인해 jsp에서 따로 선언하지 않는다.  
	String name = (String) session.getAttribute("이름");
	String class_s = (String) session.getAttribute("classA");	
%>

<h1>ShowSession</h1>
이름 : <%=name %>
과정 : <%=class_s %> ${classA}
<!-- EL 표현언어를 사용하면 내장객체에 저장한 이름으로 값을 가져올 수 있음! -->


</body>
</html>

 

출처: 스마트인재개발원

728x90
반응형