1

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

}

4

1 回答 1

0

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

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

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

于 2014-05-20T09:55:26.767 回答