2

I have a method to remove a session managed bean

public void invalidMyBean()
{
    final Map<String, Object> sessionMap = FaceContext.getCurrentInstance().getExternalContext().getSessionMap();
    sessionMap.remove("mySessionBean");
}

invalidMyBean() is also called from a Web Service. In this case FaceContext.getCurrentInstance() is null, and I can't remove my bean. I tried to store sessionMap as a field in my class, but removing from this object does'nt work. Is there a way to retrieve sessionMap outside from a faceContext ?

thx

2 Answers2

2

The ExternalContext#getSessionMap() is just an abstraction of HttpSession#get/set/removeAttribute(). So wherever you are in the servletcontainer (filter, servlet, webservice, whatever), once you've got a hand of the concrete HttpSession instance, then you should be able to use session.removeAttribute("mySessionBean") on it.

Note that this obviously only works when the webservice is been requested using the same HTTP session as the JSF application (the way you put this question — you seem to not understand at all how HTTP sessions work — suggests that this is not the case).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • the http session of the web service is different (the web service client is an applet in the jsf page). we use sessionID to know which applet match with the Facecontext, so we have one sessionID (String) and two httpSessions. the method invalidMyBean() is in a shared object between applet and jsf sessions. Maybe it is possible to retrieve httpSession object from sessionID ? – user1826704 Nov 15 '12 at 15:26
  • Add the `jsessionid` path parameter to the URL to let the applet maintain the same HTTP session. For an related answer, see also http://stackoverflow.com/questions/13194034/accessing-jsf-session-scoped-bean-from-servlet-which-is-called-by-applet-embedde/13194214#13194214 – BalusC Nov 15 '12 at 15:30
0

I answer to myself giving the working code for an applet in a JSF page sharing the same HttpSession. The applet talks to a web service on the server using JAX-WS.

in JSF page :

<applet ..>
<param name="commonSessionId" value="#{session.id}" />
Distance Sensor [Your browser doesn&rsquo;t seem to support Java applets.]
</applet>

in applet init() :

@Override
public void init()
{
    ...
    commonSessionId = getParameter("commonSessionId");
    port = service.getWsAppletPort();
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("Cookie", Collections.singletonList("JSESSIONID=" + commonSessionId));
    final Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();
    requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, map);
    requestContext.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, Boolean.TRUE);
}

thanks again to BalusC for his great help !!