1

Good day fellas, I'm a confused about which session bean am to use for my application. I'm trying to build a mobile web app like Facebook which can allow multiple users at the same time. I surf the internet to get more information. From the info I gathered from stack overflow and other tutorials, is that a Stateful session beans maintain state both within and between transactions (conversational state) and that its meant for a client. Stateless does not but support multiple clients to pool the bean instance. Whilst Singleton is a little similar to Stateless bean.

My question is which session bean am I to use for the application. Thanks for your quick response.

NB: Client (Mobile phone) communicate with the servlet and the servlet communicates with EJB to pull data from the database.

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();
    }
}

}

user3655892
  • 13
  • 1
  • 6
  • Possibly all types; you don't use one tool to build a house so to say. You can't learn this just by trying stuff, EJB technology is deceptively easy to program yet incredibly hard to apply correctly. Get a good book on EJB tech and prevent burning yourself on this. But also consider this: perhaps you don't need EJBs at all. – Gimby May 20 '14 at 09:51

1 Answers1

0

You would need a singleton bean for all classes that do not preserve a state. In your case, for database communication, you need a singleton.

For user authentication, typically you store the user object or a user token somewhere in the session itself. Depending on your implementation you will have some kind of SessionContext/SessionStore where the login state is maintained.

You don't need session scoped beans for maintaining a user session!

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Thanks for the answer @membersound, but the LoginServlet is just one of the servlets I created, I have series of servlets that communicate with just one EJB for persistence. My fear is how to curb multiple client connection with no problem since its possible to have client communicating with the server simultanously – user3655892 May 20 '14 at 10:08