시소당
import java.util.*;
import javax.servlet.http.*;
public class SessionDao {
protected HttpServletRequest req;
protected HttpServletResponse res;
protected HttpSession session;
public String mb_name;
public String mb_code;
public String mb_grade;
public String mb_admin;
/**
* SessionDao 생성자
* @param request
* @param response
*/
public SessionDao(HttpServletRequest request, HttpServletResponse response){
try{
req = request;
res = response;
session = req.getSession();
mb_name = null;
mb_code = null;
mb_grade = null;
mb_admin = null;
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
/**
* 세션 저장
* @param key
* @param value
*/
public void setSessionValue(String key, Object value){
try{
Log.debug(key, this);
session.setAttribute(key, value);
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
/**
* 세션 값 반환
* @param key
* @return
*/
public Object getSessionValue(String key){
Object obj = null;
try{
obj = session.getAttribute(key);
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return obj;
}
/**
* 세션 삭제
*/
public void delAllSession(){
try{
session.invalidate();
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
/**
* 세션값을 Map으로 리턴
* @return
*/
public Map getSessionMap(){
//StringBuffer sessionAll = null;
Enumeration enumer = null;
Map sessionAll = new HashMap();
try{
enumer = session.getAttributeNames();
while(enumer.hasMoreElements()){
String key = (String)enumer.nextElement();
sessionAll.put(key, session.getAttribute(key));
}
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return sessionAll;
}
/**
* mb_name 세션 저장
* @param mb_name
*/
public void setMbName(String mb_name){
try{
Log.debug("Member_Name"+mb_name, this);
session.setAttribute("Member_Name", mb_name);
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
/**
* mb_code 세션저장
* @param mb_code
*/
public void setMbCode(String mb_code){
try{
Log.debug("Member_Code"+mb_code, this);
session.setAttribute("Member_Code", mb_code);
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
/**
* mb_grade 세션 저장
* @param mb_grade
*/
public void setMbGrade(String mb_grade){
try{
Log.debug("Member_Grade"+mb_grade, this);
session.setAttribute("Member_Grade", mb_grade);
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
/**
* mb_admin 세션 저장
* @param mb_admin
*/
public void setMbAdmin(String mb_admin){
try{
Log.debug("Member_Admin"+mb_admin, this);
session.setAttribute("Member_Admin", mb_admin);
}catch(Exception e){
Log.error(e.toString(), e, this);
}
}
public String getMbName(){
Object obj = null;
try{
obj = session.getAttribute("Member_Name");
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return (String)obj;
}
public String getMbCode(){
Object obj = null;
try{
obj = session.getAttribute("Member_Code");
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return (String)obj;
}
public String getMbGrade(){
Object obj = null;
try{
obj = session.getAttribute("Member_Grade");
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return (String)obj;
}
public String getMbAdmin(){
Object obj = null;
try{
obj = session.getAttribute("Member_Admin");
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return (String)obj;
}
/**
* 저장 되어잇는 모든 세션값 보기
* @return sessionAll
*/
public String viewSession(){
StringBuffer sessionAll = new StringBuffer();
Enumeration enumer = null;
try{
enumer = session.getAttributeNames();
sessionAll.append("================= session Start ===================");
while(enumer.hasMoreElements()){
String key = (String)enumer.nextElement();
sessionAll.append("\n "+key+" : "+ (String)session.getAttribute(key));
}
sessionAll.append("================= session End ===================");
}catch(Exception e){
Log.error(e.toString(), e, this);
}
return sessionAll.toString();
}
}
위의 소스 코드는 저희 회사에서 사용하는 환경인 자바 jdk1.4.x 버젼과 톰캣에서
작성 되었습니다.
우선 HttpServletRequest , HttpServletResponse 를 사용하기 위하여
javax.servlet.http를 imporrt 하였습니다.
javax.servlet.http의 경우 servlet.jar 라이브러리에 포함되어 있습니다.
그리고 HttpServletRequest , HttpServletResponse 을 받아오는 부분으로는
생성자에서 받아와 초기화 시키도록 작성하였습니다.
세션 저장을 하기위하여 session.setAttribute(key, value); 이 메소드를 사용하였습니다.
세션의 값을 가져오기 위해서는 session.getAttribute(key); 를 사용하였습니다.
그리고 세션 삭제를 위하여 session.invalidate(); 를 사용하였습니다.
나머지 부분은 다들 아실꺼라 생각이 들어 넘어가겠습니다.
참.. getSessionMap()를 보시면 전체 세션의 key와 value값을 순서대로 가져와
Map으로 저장하여 return하는 방법이 있으니 참고하시기 바랍니다.
출처 : http://silentvoyage.tistory.com/95