세션생성,삭제하는 법

actionContext로 구하는 법과 SessionAware로 구하는 법,
httpServletRequest로 구하는 법을 정리합니다.

1. actionContext로 구하는 법
<세션생성>

      import com.opensymphony.xwork.ActionContext;

      Map session = ActionContext.getContext().getSession();
      session.put("session_id", id);

<세션삭제>
     Map session = ActionContext.getContext().getSession();
     session.remove("session_id");


2. SessionAware 인터페이스를 구현하면 session을 얻을수 있다

<세션생성>
import com.opensymphony.xwork.ActionSupport;
import org.apache.webwork.interceptor.SessionAware;
import java.util.Map;
public class SampleForm extends ActionSupport implements SessionAware{

  private Map session;
  public String execute() throws Exception {
      session.put("session-userid", id);
  }
}

3. HttpServletRequest로 session 생성
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
public class RequestSessionTestAction extends ActionSupport {
 
 private String id = null;
 
 public String execute() throws Exception {
  ActionContext ctx = ActionContext.getContext();
  HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
  HttpSession session = request.getSession(false);
  session.setAttribute("session-userid", id);
 
  return SUCCESS;
 }

}

댓글을 달아 주세요