【问题标题】:When to use Session Bean何时使用会话 Bean
【发布时间】:2014-07-08 12:07:32
【问题描述】:

大家好,我对要为我的应用程序使用哪个会话 bean 感到困惑。我正在尝试构建一个像 Facebook 这样的移动网络应用程序,它可以同时允许多个用户。我上网以获取更多信息。从我从堆栈溢出和其他教程中收集的信息来看,有状态会话 bean 在事务内部和事务之间保持状态(会话状态),并且它对客户端意味着。 Stateless 不支持多个客户端来池化 bean 实例。虽然 Singleton 有点类似于 Stateless bean。

我的问题是我要为应用程序使用哪个会话 bean。感谢您的快速回复。

注意:客户端(手机)与 servlet 通信,servlet 与 EJB 通信以从数据库中提取数据。

public class LoginServlet extends HttpServlet {

@EJB
CampusianDataBaseBeanLocal campusianDataBaseBean;

Campusian campusian;
Gson gson;


@Override
public void init() throws ServletException {
    super.init();
    campusian = new Campusian();
    gson = new Gson();
}


/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        String username = request.getParameter("campusianUserName");
        String password = request.getParameter("campusianPassword");
        String gsonString;

        campusian.setUserName(username);
        campusian.setPassword(password);

        System.out.println("First time: "+username);

        /**
         * This check if the username and password entered by user is correct.
         * If yes set campusian setSuccess to true and convert the object to string using gson
         * Else set object campusian method to false
         */
        if (campusianDataBaseBean.login(campusian)) {
            campusian.setSuccess(true);
            System.out.println("Connected to the database");
            /**
            try {
                connection.connect();
                connection.login(username, password);
                if (connection.isAuthenticated()) {
                    System.out.println("Connected: "+connection.getServiceName());
                    campusian.setConnection(connection);
                    campusian.setSuccess(true);
                }else {
                    campusian.setSuccess(false);
                }
            } catch (XMPPException ex) {
                Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            **/
            gsonString = gson.toJson(campusian);
        }else {
            campusian.setSuccess(false);
            gsonString = gson.toJson(campusian);
        }

        //this sends the gson string to the mobile user
        out.print(gsonString);

    } finally {            
        out.close();
    }
}

}

【问题讨论】:

  • 可能所有类型;可以这么说,您不会使用一种工具来建造房屋。你不能仅仅通过尝试来学习这一点,EJB 技术看似容易编程,但很难正确应用。获取一本关于 EJB 技术的好书,并避免在这方面烧毁自己。但也要考虑一下:也许您根本不需要 EJB。

标签: java servlets ejb-3.0 session-bean


【解决方案1】:

对于所有不保留状态的类,您需要一个单例 bean。在您的情况下,对于数据库通信,您需要一个单例。

对于用户身份验证,通常您将用户对象或用户令牌存储在会话本身的某个位置。根据您的实现,您将拥有某种 SessionContext/SessionStore 来维护登录状态。

您不需要会话范围的 bean 来维护用户会话!

【讨论】:

  • 感谢@membersound 的回答,但LoginServlet 只是我创建的servlet 之一,我有一系列servlet 只与一个EJB 通信以实现持久性。我担心的是如何毫无问题地遏制多个客户端连接,因为它可能让客户端同时与服务器通信
猜你喜欢
  • 2013-08-02
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
相关资源
最近更新 更多