「Java」getSession()でセッションオブジェクト作成、削除、取得のサンプル

1.インタフェースの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public interface SAMPLE_DATA {
//セッション保存用key
public String USERID_KEY ="USERID_KEY";
}
public interface SAMPLE_DATA { //セッション保存用key public String USERID_KEY ="USERID_KEY"; }
public interface SAMPLE_DATA {
  //セッション保存用key
  public String USERID_KEY ="USERID_KEY";
}

2.setAttributeでセッションの設定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public String funcA(){
getSession().setAttribute(SAMPLE_DATA.USERID_KEY,"xxxx" );
//some code
}
public String funcA(){ getSession().setAttribute(SAMPLE_DATA.USERID_KEY,"xxxx" ); //some code }
public String funcA(){
   getSession().setAttribute(SAMPLE_DATA.USERID_KEY,"xxxx" );
      //some code
} 

3.removeAttributeでセッションの削除

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public String funcB() {
getSession().removeAttribute(SAMPLE_DATA.USERID_KEY);
//some code
}
public String funcB() { getSession().removeAttribute(SAMPLE_DATA.USERID_KEY); //some code }
public String funcB() {
  getSession().removeAttribute(SAMPLE_DATA.USERID_KEY);
   //some code
}

4.getAttributeでセッションの利用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public String funcC(){
String sesRes =getSession().getAttribute(SAMPLE_DATA.USERID_KEY);
//some code
}
public String funcC(){ String sesRes =getSession().getAttribute(SAMPLE_DATA.USERID_KEY); //some code }
public String funcC(){
  String sesRes =getSession().getAttribute(SAMPLE_DATA.USERID_KEY);
  //some code
}

 

Java

Posted by arkgame