Discussions

EJB design: Stateful Session Bean Question

  1. Stateful Session Bean Question (13 messages)

    I'm working on a web application, using EJBs, java clases and JSPs
    Many users can be log on the application at the same time.

    I use a "Stateful Session Bean" EJB 2.0 to store the user information during the session

    The remote interface name is: ApasSession
    The home interface name is: ApasSessionHome
    The bean name is: ApasSessionBean

    In the indexBean I call the method create() of the Sateful Session Bean, and set the information of the user that I obtain from a JSP form, I also call a function call "setUserInformation(password, user)", that make a consult to a database and store other information of the user on the session bean:

    ...
        ApasSession apasSession = null;

        try {

        ApasSessionHome apasSessionHome = null;

          Context context = null;
          Object homeObject = null;

          context = getInitialContext();

          homeObject = context.lookup("apas.ApasSession");


          apasSessionHome = (ApasSessionHome)
              PortableRemoteObject.
              narrow(homeObject, ApasSessionHome.class);

          apasSession = apasSessionHome.create();


          apasSession.setPassword(password);
          apasSession.setUser(user);
          setUserInformation(password, user);//Information from the dataBase.

        }
        catch (Exception ex) {
    error += ex.getMessage();
        }
    ...

    my question is: how can I access the session Bean from other class or a JSP, how I have to do the instantiate to call the methods getters and setters of the session bean, without lost the information that the bean alredy have? For example I want to display: current user "name" on every JSP, that information is on the Session Bean variables, how can I get access to it?, Other example is that I want to make a consult to a database and I have to access a variable of the SessionBean to get a parameter for the database query how I can access that variable of the Session Bean?

    Thanks

    Threaded Messages (13)

  2. Stateful Session Bean Question[ Go to top ]

    Normal way would bo to pass the remote interface or (better) the EJB handle to the component where you need the stateful bean. Usually you do this by saving the handle in the HTTP session.

    Kind regards,

    Andreas
    http://www.triona.de/
  3. Stateful Session Bean Question[ Go to top ]

    Hummm, I'm new in this do you have an example code that show me how to save the session variables on a httpSession and then access it from any class. I really need to know how to do that. Thanks!
  4. Stateful Session Bean Question[ Go to top ]

    Ok I DO THIS:

    ON THE INDEX JSP:

    <%@ page import="model.ApasSession"%>
    <jsp:useBean id="indexBeanId" scope="session" class="controller.IndexBean"/>

    <%
    String user = null;
    usser = request.getParameter("formUsser");

            ApasSession apasSession = indexBeanId.instanciaSessionBean(user);
            session.setAttribute("ApasSession",apasSession);

    %>

    THIS IS THE "instanciaSessionBean" FUNCTION ON THE indexBean, THAT CALLS THE CREATE METHOD WITH THE USER:

      public ApasSession instanciaSessionBean(String user)
      {
        try {
          ApasSessionHome apasSessionHome = null;
          Context context = null;
          Object homeObject = null;

          context = getInitialContext();

          homeObject = context.lookup("apas.ApasSession");

          apasSessionHome = (ApasSessionHome)
              PortableRemoteObject.
              narrow(homeObject, ApasSessionHome.class);

          apasSession = apasSessionHome.create(user);

        }
        catch (Exception ex) {
           error += ex.getMessage();
        }
          return apasSession;
      }

    ON ANY JSP:

    <%

    ApasSession apasSession = (ApasSession)session.getAttribute("ApasSession");
    String usser = apasSession.getUsser();

    %>
    <div align="right"><font size="2" face="Arial, Helvetica, sans-serif"><strong>Usser: <%=usser%> </strong></font> </div>

    ALL WORKS WELL, BUT WHEN I ACCESS THE APPLICATION WITH 2 USERS AT THE SAME TIME THE INFORMATION OF THE FIRST USER IS REMPLACE WITH THE INFORMATION OF THE NEW USER ON THE SESSION, I CALL THE CREATE METHOD OF THE STATEFUL SESSION BEAN FOR EACH USER, WHICH IS THE ERROR, AND HOW I CAN SUPPORT MULTIPLE USERS ON MY APPLICATION?

    REGARDS DANIEL
  5. it's a useBean tag problem[ Go to top ]

    use are facing this problem because u are using useBean Tag.

    <jsp:useBean id="indexBeanId" scope="session" class="controller.IndexBean"/>

    this tag genrates the instance of ur bean for the very first time and when it is executed again it searches for the alredy existing object for this bean,if it founds any then simpley uses the same old instance of the bean.
    this tag doesn't creat new object each time it is executed.....
    so you should instatiate ur bean (or get the home interface of ur bean and then call it's creat method to get ur bean) in normal way.
    Hope this works.
    best of luck
    SAJJAD AHMED PARACHA
  6. Stateful Session Bean Question[ Go to top ]

    Thanks.

    I don´t understand, I call a create method for each user that use the application why the users are using the same variables!! I really need help, this is a project for my word and I need to finnish.

    Help Please!
  7. Stateful Session Bean Question[ Go to top ]

    ok man!
    don't worry that each user is calling the creat method on ur bean to get a new instance of the bean, as if u are using stateless session bean the container is already managing a pool for the EJBs, and it will assign an EJB object to that client (calling creat method on ur bean) from pool.obviously the EJB pooling is container's work u onaly have to set it (see ur container's documentation for that)or a more advance server may automatically creat a pool (at startup).
    NOTE : Advance servers also support dynamic pool size that is they can dynamically change the size of a pool depending on the load(number of clients calling create method).
    container does same with statefull session bean but before assigning an instance of stateless session bean to a new client container passivates it's sate to a datastore(hard disk,file,database etc) and container can activate this state in future.


    SO ALL U NEED IS TO DON'T WORRY ABOUT THIS ALL :),ALSO MAY BE U NEED TO SEE SOME GOOD EJB BOOK (I READ MASTRING ENTERPRISE JAVABEANS BY ED ROMAN AND IT'S QUITE EASY TO UNDERSTAND).

    hope this will help you
    wishing you best of luck for ur project
    SAJJAD AHMED PARACHA
  8. Stateful Session Bean Question[ Go to top ]

    Daniel,

    In my opinion:
    I would not access an ejb from a jsp. There are hundreds of resources about this. I would reconsider the architecture of your application. In J2EE application that uses (jsp servlet ejb) you want to use JSP for presentation free from any java code as much as you can. Use a servlet to control your application's flow. And JavaBeans, EJB or POJO (java classes) to perform the business logic. You can accomplish all of this using the MVC pattern where you would create a separation between presentation, controller and model tiers of your application. Struts, Spring are two popular frameworks that you could consider. There are also some design patterns that you need to familiarize yourself with before you start coding J2EE applications.

    Business Delegate pattern
    Service locator pattern
    Session facade pattern
    Data Access Object pattern
    Value Object/ Transfer Object pattern
    http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html

    Daniel, I know this is a lot of information to absorb at once. But it will pay off in terms of better J2EE Applications.
  9. Stateful Session Bean Question[ Go to top ]

    Hi

    Yes, actually I'm studing the framewor of struts, but I do this application without it and is too big to change everything, this application also is based on MVC but without struts, in the View I have JSPs, in the controller java clases and in the model the EJBs, when the user make a consult the jsp take the information, and check for errors, if there is no errors it pass the information to the controller, in the controller I call instantiate the EJBs to call the finders or selects of the EJBs, the EJBs make the consult to a dataBase, then send back the consult to the controller, the controller check for errors (example: and empty answer from the model), if there is not error it pass the consult to the JSP and it show everything to the user.


    Someone say me that what I have to do to maintain the session of each user is use the DTO pattern with a stateless session bean, that is that in the session bean I have to make a function that store the variables on a javaBean (DTO) and then I can access the variables from a JSP o java class, Do you have an example of how do this (sample code), I REALLY NEEED HELP, I have to finnish this for start completelly with struts, and that is what i want.
  10. Stateful Session Bean Question[ Go to top ]

    Daniel,

    If I understood your question right. I think what you want to do is make your controller a servlet as opposed to a regular java class. that way you get access to HttpSessionRequest where you can store the DTO that is returned by your stateless session bean's methods. the servlet contianer will maintain for one session per user.
  11. Stateful Session Bean Question[ Go to top ]

    Hi Ahmed

    First thanks for your help.

    Ok this is what I want in a few works: I want in some way store the information of each user in the session (concurrent access of users) for access this information from JSPs or java clases.

    Example:

    The user call "user1" enter the application, I select the information of this user from some tables on the database, for example i select the work code where this user work. Then I want to save the information of this user in some place to access it from any jsp or java class. For example I want to call a finder of the model from the controller and i need the work code to send like parameter to the finder, or I need to display the work code on every jsp. All my application works fine, the only problem is when more than one user is in the application, beacause in the begginer I was store the information of the user on a sigleton class, that I can access from any java class o jsp, but the problem is when a "user2" enter to the application, and beacuse the variables in the singleton class of the "user1" are erase for the new variables of the "user2". Then I think on a session bean. I really don´t know to solve the problem?

    hey Ahmed do you have a messenger account on hotmail?

    thanks for all
  12. Stateful Session Bean Question[ Go to top ]

    Daniel

    if you are controller uses HttpServletRequest you can do this

    public Controller extends HttpServlet {
       public void doGet(HttpServletRequest request, HttpServletResponse response) {
       
        HttpSession session = request.getSession();
        
        // session is where you store user information

        
        String action = request.getServletPath();

        if(action.equals("findUserID")){
          // this is not the correct syntax. but you get the idea
             String userid = UserSessionBean.findUserID(String name);
           // now you can store userid in the session like so:
           session.setAttribute("userid", userid);
            try {
            request.getRequestDispatcher(path).forward(request,response);
        } catch(Exception ex) {
          ex.printStackTrace();
        }
      }
    //=================

    if you need to access userid. you can do the following in your jsp

    <% String userid = (String)session.getAttribute("userid"); %>

    That's all, the above code is by no mean ready for prime time. I am just trying to illustrate a point.

    if you use struts things will be much clearer. I hope this helps

    A
  13. Stateful Session Bean Question[ Go to top ]

    I DO IT I USE A TO PATTERN AND A HELPER AND ALL WORKS FINE, THANKS FOR YOUR HELP
  14. How did you get it to work[ Go to top ]

    can you tell me what you did i need to achieve this same functionality in my application