request.get Sessionとrequest.get Sessionの違い

6121 ワード

1.回転:http://wenda.so.com/q/1366414933061950?src=150
要約:
request.get Session:セッションがあるとセッションに戻ります。そうでなければ新しいセッションを作成します。
request.get Session(false):セッションがあればセッションに戻ります。そうでなければNULLに戻ります。
==============================================================================================================================
2.回転:http://blog.csdn.net/gaolinwu/article/details/7285783
一、需要の原因
現実にはよく次の3つの使い方があります。
HttpSession session = request.getSession();

HttpSession session = request.getSession(true);

HttpSession session = request.getSession(false);
二、違います
1.Servlet公式文書によると、
public HttpSessiongetSession(boolean create) 


Returns the currentHttpSession associated with this request or, if if there is no current sessionand create is true, returns a new session. 
If create is falseand the request has no valid HttpSession, this method returns null. 
To make sure thesession is properly maintained, you must call this method before the responseis committed. If the container is using cookies to maintain session integrityand is asked to create a new session when the response is committed, anIllegalStateException is thrown. 
Parameters: true -to create a new session for this request if necessary; false to return null ifthere's no current session 
Returns: theHttpSession associated with this request or null if create is false and therequest has no valid session 
2.翻訳してきた意味は:
getSession(boolean create)       reqeust  HttpSession ,    reqeust  HttpSession  null, create true,       Session,    null; 
    : 
HttpServletRequest.getSession(ture)    HttpServletRequest.getSession() 
HttpServletRequest.getSession(false)        Session    null; 
3.使用
Sessionに登録情報をアクセスする時、一般的な提案:HttpSession session=request.get Session()
Sessionから登録情報を取得する場合、一般的な提案:HttpSession session=request.get Session(false);
4.より簡潔な方法
もしあなたのプロジェクトでSpringを使ったら(もちろん、大きなプロジェクトは全部使いました)、sessionの操作に便利です。Sessionで値を取る必要があれば、WebUtilsツール(org.spring frame eweet.web.util.WebUtils)のget Session Attribute(HttpServletRequestrequest,String name)方法を使ってソースを見てください。
publicstatic Object getSessionAttribute(HttpServletRequest request, String name){
  Assert.notNull(request, "Request must not be null");
  HttpSession session =request.getSession(false);
  return (session != null ?session.getAttribute(name) : null);
}
注:AsssertはSpringツールバッグの一つで、いくつかの検証操作を判断するために使用されます。この例では、reqeustが空かどうかを判断するために使用されます。空ならば、例外を投げます。
あなたが使用する時:WebUtils.set Session Attribute;
        User user=(User)WebUtils.get Session Attribute;
ソース:
/**
 * Set the session attribute with the given name to the given value.
 * Removes the session attribute if value is null, if a session existed at all.
 * Does not create a new session if not necessary!
 * @param request current HTTP request
 * @param name the name of the session attribute
 */
public static void setSessionAttribute(HttpServletRequest request, String name, Object value) {
  if (value != null) {
    request.getSession().setAttribute(name, value);
  } else {
    HttpSession session = request.getSession(false);
    if (session != null) {
      session.removeAttribute(name);
    }
  }
}
三、運転結果
以上の例はテストに合格しました。