Killing a bean with application scope?

Is there any way to kill a bean with application scope?
I am writing a web application, that most likely the browser window will never be closed. The problem is a user is authenticated onto our site but because the bean containing the users acct info is application scope, if another user logs in it still shows the old user as logged in. So on logout i would like to kill the application, the bean, servletContext...anything that will refresh the bean and the corresponding jsp. Any help is much appreciated.

There is an indirect way if you know servlets. The "application" implicit object that you use in your JSP is the javax.servlet.ServletContext object. Instead of declaring your bean using the jsp:useBean tag, directly put your objects into the ServletContext using ServletContext.putAttribute() method. Objects put in the ServletCOntext are available to all pages in the application. Just use the ServletContext.getAttribute() method to retrieve this bean in any other page.
To "kill the bean", just reset the value of this object to null by calling putAttribute(null).

Similar Messages

  • Share stateful session bean in JSF managed beans with different scope

    Hi,
    I have a JSF application and I want to try to use of stateful session beans.
    So I created a new stateful session bean and its local interface.
    @Stateful
    public class StatefulSessionBean implements StatefulSessionBeanLocalInterface{
    private String name;
    @Local
    public interface StatefulSessionBeanLocalInterface {
    ...In my JSF application I have a mananed bean with session context which registers the new interface by
    this annotation
    @EJB(name="sessionbeanref", beanInterface=StatefulSessionBeanLocalInterface.class) and set the name to something.
    Now I want to fetch this name in another managed bean with request scope. So I looked up the bean and tried to get the name.
    StatefulSessionBeanLocalInterface = (StatefulSessionBeanLocalInterface) new InitialContext().lookup("java:comp/env/sessionbeanref");
    System.out.println(currentmailingbean.getName());but the name is null.
    Why?

    The xsd was created via the netbeans J2EE enterprise application dialog and I think its the most recent.
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">All other annotations seem to work.
    Wouldnt the lookup completely fail if the deployment process thought that it is version 1.4 ?

  • Setting Beans in Application Scope

    How do I set beans in application scope at the start of a JSF web app? I'd like to be able to do something similar to the Struts plugin.

    Hi,
    I also tried to use a servletcontextlistener. The problem is, that I cannot
    put any beans in the facescontext, because a NullpointerExcpetion
    is thrown. In the contextInitialized-Method I do the following:
    FacesContext context = FacesContext.getCurrentInstance();
    ApplicationFactory factory =
         (ApplicationFactory) FactoryFinder.getFactory(
                   FactoryFinder.APPLICATION_FACTORY);
    Application application = factory.getApplication();
    ValueBinding binding = application.createValueBinding( "#{Bean}" );
    In the createValueBinding-method a Exception is thrown. So I cannot call
    createValueBinding when I am not in a facescontext.
    Can anyone help me?
    Thanks
    Andy

  • Destroy in JavaBean with application scope?

    Hi,
    I am using a java bean in my jsp application with scope "application". I know that this java bean will be terminated only when the server stops or shuts down (Apache Tomcat 5.5).
    I need to cleanup something manually when the server shuts down. Can anyone tell me what method will be called, or that I can override, to clean up?
    I tried finalize and valueUnbound (implements HttpSessionBindingListener) with no luck... I just can't find a equivalent to destroy in Servlets (I can't use servlets, must use javabeans+jsp)...
    Any sugestion?
    thanks in advance for your time

    There is an indirect way if you know servlets. The "application" implicit object that you use in your JSP is the javax.servlet.ServletContext object. Instead of declaring your bean using the jsp:useBean tag, directly put your objects into the ServletContext using ServletContext.putAttribute() method. Objects put in the ServletCOntext are available to all pages in the application. Just use the ServletContext.getAttribute() method to retrieve this bean in any other page.
    To "kill the bean", just reset the value of this object to null by calling putAttribute(null).

  • Updating Beans in Application Scope

    The problem is that i dont know how to update a bean in an application scope:
    in EA4 i use this instruction to update a bean property:
    // get the faces context
    FacesContext facesContext = FacesContext.getCurrentInstance();
    // update a property in application scope
    (Util.getValueBinding("BeanName.propertyName")).
    setValue(facesContext,"text");
    and it worked ok in jsp:
    <h:output_text valueRef="BeanName.propertyName" />
    Now in version 1.0 i wrote:
    Application app = facesContext.getApplication();
    errorAplicacion = (ErrorAplicacion)app.createValueBinding("#{beanName}").
    getValue(facesContext);
    How should i update this value in application scope to be used in the jsp ?
    I use in jsp:
    <h:output_text value="#{BeanName.beanProperty}"/>
    But i receive in logs:
    Expression BeanName.beanProperty does not follow the syntax #{...}
    And the result is null
    Thanks a lot
    German

    If you are quoting your 1.0 code literally, you've got a case mismatch between "#{beanName}" when you create the value binding expression and "#{BeanName}" in the JSP page. As with most things in Java, the names are case sensitive.
    Craig McClanahan

  • Reset JSF session and the managed beans with sesison scope

    Hi,
    this is a very general question and maybe stupid for most of you. I have my jsf/facelets web application and i use inside of this application some managed beans, which are session beans. I want to know how is it possible to reset this beans. I'm asking this question beacuse i have this kind of problem: i built my web application which has a login form and i use the browser to test it. When i browse to the login page and I login with my credentials i get my customized home page. Then i open another istance of the browser and i browse to the login page again but this time i login as a different user. The result home page is the same as i got before with my login credentials, so the session is always the same. Instead i want the session and all its objects to be resetted for the new user! Do youn know which is the solution?

    The fact is that i want to have two sessions in parallel, so using the same browser and opening two tabs, i want to browse to the login page and access as two totaly different users and using in parallel the application without the problem of one user's action affecting the other user beacuse of session sharing. So I want to force the application to create two different session for the two users logins, because as i told you before as it is now, they are sharing the same sesison. And i think that if i at the login time I iterate thorugh the session and delete all the objects i will be able to have only one session per time. Isn't it?

  • Jsp:useBean : Missing value of String classed bean with 'session' scope

    Hi!
    I'd like to ask some help.
    I have these two JSP pages:
    1.jsp<jsp:useBean id="str" class="java.lang.String" scope="session"/>
    <html>
    <body>
    <% str="hello"; %>
    <a href="2.jsp">click</a>
    </body>
    </html>
    2.jsp<jsp:useBean id="str" class="java.lang.String" scope="session"/>
    <html>
    <body>
    <%=str%>
    </body>
    </html>When I open 1.jsp in my browser, then click on the link, the result is "nothing" (empty string). Why does the bean lose its value on the way?
    I use a Tomcat 5.5.9 server.
    Any help will be highly appreciated.

    You have to think of several scopes when working with JSP. The first is the local scope: the method _jspService() where all the work of the JSP is done.  This acts as a normal method and is where all the sciptlet code goes.
    When you use jsp:useBean you are creating two references to a new String object. One in the local scope accessible through <%= str %> and the other in the session scope.
    When you do <% str = "hello"; %> you are changing the local str variable to reference the String "hello" (this is equivalant to doing <% str = new String("hello"); %>). Only the local reference is changed, not the second reference in session.
    If you want the change to take affect, then you will have to store the new value in session with the same name:
    <% session.setAttribute("str", str); %>

  • Application scope bean Vs. EJB

    I want to cash some application data - and was thinking of using a singleton bean with application scope in my controller and JSP page.
    <jsp:useBean id="myBean" scope="application" class="bean.datacash" />
    I am not very familiar with EJBs - and wanted to know if it would be a mistake not to use an EJB to perform that task.
    Is there a downside to my approach?
    Thanks for any insight
    Mike

    Entity EJB'S are used as a kind of data persistence, mostly to Databases. In a way it's like a kind of cache but any changes you make to an EJB are usually persisted to the underlying data storage on a regular basis.
    Session EJB's are more for business logic and depending on the type of Session Bean you use can either remain persisted in a session state or used and thrown away.
    EJB's also require alot of code so depending on your application requirements it is kind of a big decision.
    Is the data your wanting to cache to be shared by all users using the application? Are there any race conditions that may be met by concurrent access to the data? What do you plan to do with the cached data store it in a database or just make it available to share?
    If you want to look into using EJB's then your going to require an EJB Container to run them in (this can be standalone like OpenEJB or as part of an J2EE Application server like JBoss or Oracle Application Server to name just a couple).
    Using the Singleton Javabean can keeps things simple but once you start to synchronize methods with multiple users you can find some slowing down of the logic as the singleton tries to complete a request before getting onto the next.
    The [url http://jakarta.apache.org]Jakarta team offer a package called commons pooling which is designed as a pooling framework (also a way of caching) that can come in handy, I've used it to create a custom JDBC Connection pooler and it works quite well when you get the hang of it.
    Another thing to consider is the load the application may be under at it's busiest time. How many concurrent users do you expect to access the application?
    Sorry I can't give you a real answer as that is entirely up to you and your requirements. If you have a small app that has around 20-50 users then EJBs are bit of an overkill. If you find you'll have 100+ users with lots of data to manage then EJB's are probably a good option.
    If you have a look at [url http://www.theserverside.com]the serverside web site you may find a couple of articles on data persistence or check out some of the online magazines which also have articles on this same topic.
    Anthony

  • Application scope java beans not behaving....

    I am using WL 5.1, sp8, on NT....
              I am running jsp code that declares java beans of application scope....
              However, it doesn't appear to be resolving the application scope
              properly, it seems to instantiate a new Bean each time I go to
              a new page within the same application....
              One thing I am not clear on, is whether I have properly configured
              the WebApp....I have just been assuming that all pages within
              the same path sub-directory are of the same application, but that
              doesn't appear to be working....
              I haven't found any weblogic.properties settings for defining WebApps,
              that will share application scope, etc....
              I assume there is a simple answer here, I appreciate any help...
              Thanks,
              Jason Rosenberg
              

    Jason,
              WLS 5.1.0 is up to service pack level 12 now, which you should go to
              http://support.bea.com/welcome.jsp and login to download from the "Supports
              Download" section. Note that you need to be a paying customer to see this
              section. After applying sp12 you should take a look at our web app docs
              here http://bernal.beasys.com/release/docs51/classdocs/webappguide.html for
              instructions on setting up a web app and registering it in wls 5.1.
              "Jason Rosenberg" <[email protected]> wrote in message
              news:[email protected]...
              > I am using WL 5.1, sp8, on NT....
              >
              > I am running jsp code that declares java beans of application scope....
              >
              > However, it doesn't appear to be resolving the application scope
              > properly, it seems to instantiate a new Bean each time I go to
              > a new page within the same application....
              >
              > One thing I am not clear on, is whether I have properly configured
              > the WebApp....I have just been assuming that all pages within
              > the same path sub-directory are of the same application, but that
              > doesn't appear to be working....
              >
              > I haven't found any weblogic.properties settings for defining WebApps,
              > that will share application scope, etc....
              >
              > I assume there is a simple answer here, I appreciate any help...
              >
              > Thanks,
              >
              > Jason Rosenberg
              >
              >
              >
              >
              >
              >
              >
              

  • Updating DB from beans with transactions

    Hello,
    I have a 4 step form to input data from a user.
    step1.jsp, step2.jsp, step3.jsp, step4.jsp, submit.jsp
    For every form, I am using a javabean (jsp:useBean) with session scope so that I can update all at the end. So in step2.jsp, I set the properties of Bean1, in step3.jsp- the properties of Bean2... and finally in submit.jsp I get all the 5 beans with session scope.
    I now want to insert values from these different beans simultaneously into a Database. (single commit)
    If I want to use Transactions, how can I get the values from these beans (Bean1, Bean2, Bean3, and Bean4) to a single class? Or can you suggest another method?
    Thanks.

    anand_agrawal wrote:
    why not just use 1 object for all pages anyway?
    why 4 beans, is it necessary?I'd like to have multiple beans. Is there a way it can be done?you should know that you can create a class to hold whatever you want
    and you should also know that you can copy data from one class to another
    so of course it's possible
    what are you asking?

  • Objects of application scope and clustering.

              I understand that weblogic 6.1 replicates session data to a secondary server in a
              cluster. This replication should include objects declared with <jsp:useBean ...
              scope="session"/> as well as objects that are explicitly maintained in the session
              with setAttribute().
              However, what about objects with application scope? Are they replicated to a secondary
              server when a cluster is in use?
              Thanks,
              -Dave.
              

    David Vazquez <[email protected]> wrote:
              > I understand that weblogic 6.1 replicates session data to a secondary server in a
              > cluster. This replication should include objects declared with <jsp:useBean ...
              > scope="session"/> as well as objects that are explicitly maintained in the session
              > with setAttribute().
              > However, what about objects with application scope? Are they replicated to a secondary
              > server when a cluster is in use?
              No, they are not replicated in the cluster. To implement replication you can use JavaGroups:
              http://sourceforge.net/projects/javagroups/ or Cameron's Coherence product:
              http://www.tangosol.com/products-clustering.jsp for example.
              > Thanks,
              > -Dave.
              Dimitri
              

  • Implementation of application scope in servlet technology

    hello,
    i want to define certain variable with application scope , in servlet technology how to do that

    Attributes can be stored in the ServletContext in the same manner as they are stored in the session.
    String x = "Stored Value";
    getServletContext().setAttribute("x", x);
    String y = (String) getServletContext().getAttribute("x");

  • Problem with beans in session scope

    Hello,
    I developped a website using JSP/Tomcat and I can't figure out how to fix this problem.
    I am using beans in session scope to know when a user is actualy logged in or not and to make some basic informations about him avaible to the pages. I then add those beans to an application scope bean that manages the users, letting me know how many are logged in, etc... I store the user beans in a Vector list.
    The problem is that the session beans never seem to be destroyed. I made a logout page which use <jsp:remove/> but all it does is to remove the bean from the scope and not actualy destroying it. I have to notify the application bean that the session is terminated so I manualy remove it from its vector list.
    But when a user just leave the site without using the logout option, it becomes a problem. Is there a way to actualy tell when a session bean is being destroyed ? I tried to check with my application bean if there are null beans in the list but it never happens, the user bean always stays in memory.
    Is there actualy a way for me to notify the application bean when the user quits the website without using the logout link ? Or is the whole design flawed ?
    Thanks in advance.
    Nicolas Jaccard

    I understand I could create a listener even with my current setup Correct, you configure listeners in web.xml and they are applicable to a whole web application irrespective of whether you use jsp or servlets or both. SessionListeners would fire when a session was created or when a session is about to be dropped.
    but I do not know how I could get a reference of the application bean in >question. Any hint ?From your earlier post, I understand that you add a UserBean to a session and then the UserBean to a vector stoed in application scope.
    Something like below,
    UserBean user = new UserBean();
    //set  bean in session scope.
    session.setAttribute("user", user);
    //add bean to a Vector stored in application scope.
    Vector v = (Vector)(getServletContext().getAttribute("userList"));
    v.add(user);If you have done it in the above fashion, you realize, dont you, that its the same object that's added to both the session and to the vector stored in application scope.
    So in your sessionDestroyed() method of your HttpSessionListener implementation,
    void sessionDestroyed(HttpSessionEvent event){
         //get a handle to the session
         HttpSession session = event.getSession();
          //get a handle to the user object
          UserBean user = (UserBean)session.getAttribute("user");
           //get a handle to the application object
          ServletContext ctx = session.getServletContext();
           //get a handle to the Vector storing the user objs in application scope
            Vector v = (Vector)ctx.getAttribute("userList");
           //now remove the object from the Vector passing in the reference to the object retrieved from the Session.
            v.removeElement(user);
    }That's it.
    Another approach would be to remove the User based on a unique identifier. Let's assume each User has a unique id (If your User has no such feature, you could still add one and set the user id to the session id that the user is associated with)
    void sessionDestroyed(HttpSessionEvent event){
         //get a handle to the session
         HttpSession session = event.getSession();
          //get a handle to the user object
          UserBean user = (UserBean)session.getAttribute("user");
           //get the unique id of the user object
           String id = user.getId();
           //get a handle to the application object
          ServletContext ctx = session.getServletContext();
           //get a handle to the Vector storing the user objs in application scope
            Vector v = (Vector)ctx.getAttribute("userList");
           //now iterate all user objects in the Vector
           for(Iterator itr = v.iterator(); itr.hasNext()) {
                   User user = (User)itr.next();               
                    if(user.getId().equals(id)) {
                           //if user's id is same as id of user retrieved from session
                           //remove the object
                           itr.remove();
    }Hope that helps,
    ram.

  • Looking for best practice on application scope beans

    Hey – a portal newbie here. I’ve got some application scope beans that need to be initialized on startup. First thought was to create a servlet that would set the bean. Then I saw the GlobalApp setting, but I think that looks like it is more session scope than application… Looking to be corrected here if I am wrong.
    Is there a place where these type of things traditionally happen? Read only, so no cluster worries (I think) Using WLP 8.1 SP4 and looking for best practices. Thanks for the help!

    To support "code sharing" you need an integrated source code control system. Several options are out there but CVS (https://www.cvshome.org/) is a nice choice, and it's completely free and it runs on Windows, Linux, and most UNIX variants.
    Your next decision is on IDE and application server. These are usually from a single "source". For instance, you can choose Oracle's JDeveloper and Deploy to Oracle Application Server; or go with free NetBeans IDE and Jakarta Tomcat; or IBM's WebSphere and their application server. Selection of IDE and AppServer will likely result in heated debates.

  • Initializing application scope beans on startup

    Hi All,
    I need to initialize an application scope managed bean on startup, and not have to wait for it to be referenced the first time.
    I was thinking of using ContextListener, but you dont have access to the faces context in the listener.
    And you cant even extend FacesServlet, since its final.
    Any ideas on how this can be achieved.
    Thanks for your help!!

    Try with FacesContext.getCurrentInstance() or http://www.jsffaq.com/Wiki.jsp?ptitle=How+to+access+FacesContext+from+the+Filter+or+Servlet%3F&page=HowToAccessFacesContextFromTheFilterOrServlet

Maybe you are looking for

  • Safari error message and will not launch

    I was trying to update my software to the new Mountain Lion but it would not complete, so i downloaded the OS X Lion again. It DL and installed, but now when i try to open Safari I am getting this error... Please help! Process:         Safari [424] P

  • Attach a (PDF) document to a calendar event?

    I'm trying to figure out a way to attach a document (in this case a PDF) to a calendar event. It's clearly not possible in the Calendar app, and I can't seem to get it to work thru Google Calendar (it shows the attachment but won't let me open it) so

  • Best way to implement FREE purchase?

    Hi, I have purchases that are free for logged in users. What's the best way to implement a no-payment solution? Would having the payment fields hidden and COD auto-selected if the amount field is 0.00 a good idea? Is there a better way anyone has imp

  • Re-using HD with XP and new mobo ??

    I know this is a dumb question but ... I have a 2-yr old ATX mobo running XP and a Athlaon 1500+ cpu.   Instead of incrementally upgrading the system, I think I will buy a new mobo and start fresh.  My question is ... can I connect up my previous HD

  • How to view my laptop project from my phone app?

    Hi, I am editing a short film on Adobe from my laptop. Is it possible for me to view it from the Adobe app on my phone? thanks