언어/Java

[Java] session 저장하기 , 가져오기, 지우기

antoroong 2025. 2. 16. 00:34

IDE : 이클립스

DB : mysql

SQL : ibatis

OS : centos 7

server : tomcat 6

jdk : 1.6

프레임워크 : 전자정부프레임워크 3.8

 

 

저장하기

public String putSession() throws Exception{
		// ActionContext에 session 메소드 꺼내기
		ActionContext context = ActionContext.getContext();						
		Map<String, Object> session = (Map<String, Object>)context.getSession();

			//session 지우기
			session.clear();
			
     //session.put("저장할session이름", 저장할 데이터);
			session.put("NAME","도로롱");	 			 // 회원 이름 
			session.put("MEMBER_TYPE","의사"); // 회원 구분
			session.put("LEVEL", "원장");
			
			context.setSession(session);	//session  저장
		}
		return SUCCESS;
	}

 

 

2.가져오기

public String importSession(){
		
		// ActionContext에 session 정보를 가져온다
		ActionContext context = ActionContext.getContext();						
		Map<String, Object> session = (Map<String, Object>)context.getSession();
		
		//putSession에서 등록했던 key값을 넣어서 get한다
		String name= (String) session.get("NAME");
		String member_type = (String) session.get("MEMBER_TYPE");
		
    //sys아웃으로 확인해보자
		System.out.println("name:"+name+",member_type :"+member_type );

		return SUCCESS;
	}

 

 

 

 

3.지우기 (로그아웃에 많이 사용함)

public String removeSession(){
		
		// ActionContext에 session 정보를 제거한다.
		ActionContext context = ActionContext.getContext();						
		Map<String, Object> session = (Map<String, Object>)context.getSession();
		
//putSession에서 등록했던 key값을 넣어서 remove한다
			session.remove("NAME");	 			 
			session.remove("MEMBER_TYPE"); 
			session.remove("LEVEL"");
		
		context.setSession(session);			
		session.clear();

		return SUCCESS;
	}