JSF : How to update one session bean base on another session bean?

I am currently modifying some jsf application. I have two beans.

  • connectionBean
  • UIBean

When I set my connection parameters in connectionBean the first time, the UIBean is able to read my connectionBean information and display the correct UI Tree.

However when I try to set the connection parameters in the same session. My UIBean will still use the previous connectionBean information.

It only will use after I invalidate the whole httpSession.

Is there anyway I can make one session bean update another session bean?

Asked by: Nicole381 | Posted: 28-01-2022

Answer 1

Sounds to me like it's some kind of problem with UIBean referencing an out-of-date version of ConnectionBean. This is one problem with JSF - if you re-create a bean, JSF will not update the references in all your other beans.

You could try getting a 'fresh' copy of the ConnectionBean each time. The following method will retrieve a backing bean by name:

protected Object getBackingBean( String name )
    FacesContext context = FacesContext.getCurrentInstance();

    return context
            .getApplication().createValueBinding( String.format( "#{%s}", name ) ).getValue( context );

Without knowing the specifics of your code and how you're using the beans it's difficult to be more specific!

Answered by: Tess892 | Posted: 01-03-2022

Answer 2

@Phill Sacre getApplication().createValueBinding is now deprecated. Use this function instead for JSF 1.2. To get a fresh copy of the bean.

protected Object getBackingBean( String name )
    FacesContext context = FacesContext.getCurrentInstance();

    Application app = context.getApplication();

    ValueExpression expression = app.getExpressionFactory().createValueExpression(context.getELContext(),
            String.format("#{%s}", name), Object.class);

    return expression.getValue(context.getELContext());

Answered by: Gianna564 | Posted: 01-03-2022

Answer 3

Define constant and static method in first session bean:

public class FirstBean {

public static final String MANAGED_BEAN_NAME="firstBean";

 * @return current managed bean instance
public static FirstBean getCurrentInstance()
  FacesContext context = FacesContext.getCurrentInstance();
  return (FirstBean) context.getApplication().evaluateExpressionGet(context, "#{" + FirstBean.MANAGED_BEAN_NAME + "}", TreeBean.class);

than use in second session bean like this:

FirstBean firstBean = FirstBean.getCurrentInstance();  

Better approach would be to use some Dependency Injection framework like JSF 2 or Spring.

Answered by: Dainton264 | Posted: 01-03-2022

Similar questions

java - why session have session id after session timeout in jsp?

Hi in jsp of JavaScript i am checking userdId in session or not it always have session id if session expires also, but i checked in java Action class after session expires userid is null but not in jsp. code link

java - Get session bean from another session in Spring

Is there a way in Spring I can access some session bean from another session? For example, can some singletone change data in session bean for two users, or one user's action change some data in session bean for the second user.

java - Can't get session scoped bean on session timeout

I need to get a proxied session-scoped bean on the HttpSessionListener.sessionDestroyed(). The objective is doing a session cleanup when it gets destroyed (either by invalidate() or timeout). I added the ContextLoaderListener to expose the context and got the bean through WebApplicationContextUtils.getWebApplicationContext(). Everything works fine if I invalidate ...

java - Getting wrong value from session in javascript, How to use session in javascript?

I am using following code to get session variable value in javascript embedded in a JSP: var numberOfPages= '<%=session.getAttribute("numberOfPage")%>'; I am getting a wrong value (old value) when I hit the page for the first time but when I refresh the page, then I'm getting the correct value. Please help me. Servlet code: HttpSession session = request.getSe...

java - Telnet session vs TCP session

What's the difference between a TELNET and TCP session? I have a simple server: public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(5555); Socket socket = ss.accept(); System.out.println("Waiting for a client..."); InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream(); DataOutputStr...

java - In Spring MVC, do I need to unbind bean objects bound to the session when I am done using the session scoped bean? If yes, how?

I am using few session scoped beans in the app context xml of my spring mvc app. I have seen invalidate() method in HTTPSession class which does the detaching thing. Do I need to call this method somewhere in my spring app also to free up the session from the session scoped beans when those beans are mo more in use? Or does Spring internally take care of this?

java - In Ejb Session bean can we extend another session bean

Program: @Local interface AInter{ //... } class A implements AInter{ //... } @Local interface BInter extends AInter{ //... } class B implements BInter { //... } In Backing: @Inject BInter binter; //It's work fine But If i tried @Inject AInter aInter; //But if i try to access A interface from my backing. It showed warning like this Multiple beans a...

java - Can I call from one ejb3 session bean method to other method in same session bean ?

Can I call from one ejb3 session bean (statless/statefull) method to other method in same session bean ? does state of members will be save between calls ?

How to Create new session with given session id in java

we have our application deployed on a multi cluster environment running with load balancer on WebSphere 8.5. Session Affinity is enabled on WebSphere. In our application, once the session expires , a new session is created with new SessionID but to make session affinity work SessionID in request header should be same as that of the initial request made to load balancer. what I want to achiev...

java - Using spring session scope without session

I have web application where I defined session scoped UserDetail. The problem is that I also have some Quartz jobs that should use UserDetail bean. When job is run I get: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.userDetails': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you inten...

java - JSF Memory Usage Issue when using using session beans

I am working on an application that has a navigation tree which is a session bean. Every time I invoke the page that contain this bean, my memory usage will increase. However, after few hours of inactivity, the memory still isn't freed. Are there any ideas why this happens, or workarounds?

java - How to check if a session is invalid

How to check if a session is invalid or not? There is no method in the API. Is it the same as isNew()? And what is the difference if not?

java - How do I change the JRun Session Timeout?

If you try to launch a JRun server and initialization takes more than 30 secs, the launch fails. Is there any way to increase this 30 second limit?

java - Clear HTTP Session

I am trying to clear everything on my HTML form when I visit it from a hyperlink entry. Any ideas? My development language is java.

java - how do you read locale information from session?

The following bit of code sets the Locale from lang param on querystring. <c:if test="${param['lang'] != null}"> <fmt:setLocale value="${param['lang']}" scope="session"/> </c:if> How do I now read that variable? I know its a bit noobish. I need a bit of conditional logic to display one language link if the local hasn't been set in the session scope usi...

java - Impact of SSO on session time out

I need to implement SSO between a windows domain and a J2EE web appliction. I've been thinking what the impact of this would be on the web application's session timeout. We have a 2 hour time out. If we implement a seemless SSO, then I think it might become confusing for the user. The SSO will make it appear that the web application is immediately available. I'm worried that they will start ...

java - Session Bean doesn't work

I have a SessionBean and want to load DATA from an InfoHolder Class called Benutzer. But it doesn't work. /* * SessionBean1.java * * Created on 21.06.2009, 16:03:41 */ package egispartnerprofile; import com.sun.rave.web.ui.appbase.AbstractSessionBean; import group52.infoholders.Benutzer; import javax.faces.FacesException; /** * <p>Session scope data bean for your app...

java - Access user session objects from another thread, how to make it?

How can I access a user session objects from another thread? I want to delete some users on a regular basis. I can just delete them from the database, but some user can be logged in. So I want to invalidate his session if its exists. How can I find and invalidate his session using his userId? As I understand a servlet container has some hash-like structure ...

java - How to pass a session between tomcat and php

Hey, I am in a WTF code situation working on a jsp tomcat server and trying to pass session data (user id, etc.) to php. i am planning to rewrite php session handling with session_set_save_handler() my question is where does tomcat stores it session data (harddrive?) and what kind of encoding does it uses? or am i on the wrong path? i know t...

session - Java Orkut Login

Closed. This question needs details or clarity. It ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)