Webアプリケーションを使用して各クライアントとの会話状態を追跡したい場合は、セッションBeanまたはHTTPセッションのどちらを使用するのが良いですか?
HTTPセッションの使用:
//request is a variable of the class javax.servlet.http.HttpServletRequest
//UserState is a POJO
HttpSession session = request.getSession(true);
UserState state = (UserState)(session.getAttribute("UserState"));
if (state == null) { //create default value .. }
String uid = state.getUID();
//now do things with the user id
セッションEJBの使用:
Webアプリケーションリスナーとして登録されたServletContextListenerの実装ではWEB-INF/web.xml
:
//UserState NOT a POJO this this time, it is
//the interface of the UserStateBean Stateful Session EJB
@EJB
private UserState userStateBean;
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("UserState", userStateBean);
...
JSPの場合:
public void jspInit() {
UserState state = (UserState)(getServletContext().getAttribute("UserState"));
...
}
同じJSPの本文の他の場所:
String uid = state.getUID();
//now do things with the user id
それらはほとんど同じであるように思われますが、主な違いは、UserStateインスタンスがHttpRequest.HttpSession
前者ServletContext
の場合と、後者の場合に転送されることです。
2つの方法のどちらがより堅牢で、なぜですか?