Results 1 to 8 of 8

Thread: session bean and other servlet

  1. #1
    Join Date
    Jul 2006
    Posts
    22

    Default session bean and other servlet

    Hello,
    At first, sorry for my poor english, perhaps it would be difficult to understand my need...

    In a web application, I would like that spring session beans be available to a servlet. To make that possible, the only way I've found is to instanciate a HttpSessionListener where I call WebApplicationContext.getBean(theBeanName) then manually add the bean in the session.

    first question : Why do I need manually add the bean in the session, when that bean is defined as a session bean in applicationContext.xml ? Why the method getBean do not do that Job ? Is there another way to force the instanciation of a session bean ?

    second question : is the use of HttpSessionListener is the only way to make session beans available to another servlet ? Is there another way where Spring will construct session bean before the servlet start ?

    here some peace of declaration :

    === web.xml

    <!-- Spring integration -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListe ner
    </listener-class>
    </listener>
    <listener>
    <listener-class>
    org.springframework.web.context.request.RequestCon textListener
    </listener-class>
    </listener>

    <!-- my custom HttpSessionListener where I create session bean -->
    <listener>
    <listener-class>essais.MySessionListener</listener-class>
    </listener>

    <!-- the servlet where I need Spring session bean -->
    <servlet>
    <servlet-name>
    com.metaparadigm.jsonrpc.JSONRPCServlet
    </servlet-name>
    <servlet-class>
    com.metaparadigm.jsonrpc.JSONRPCServlet
    </servlet-class>
    <init-param>
    <param-name>auto-session-bridge</param-name>
    <param-value>1</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>
    com.metaparadigm.jsonrpc.JSONRPCServlet
    </servlet-name>
    <url-pattern>/JSON-RPC</url-pattern>
    </servlet-mapping>

    === applicationContext.xml

    <!-- the session bean which I would like to be ready before the servlet start -->
    <bean id="JSONRPCBridge" scope="session" lazy-init="false" class="essais.MyJSONRPCBridge">
    </bean>

    === MySessionListener.java

    public synchronized void sessionCreated(HttpSessionEvent arg0)
    {
    ...
    HttpSession session = arg0.getSession();
    WebApplicationContext webAppContext = WebApplicationContextUtils.getRequiredWebApplicati onContext(session.getServletContext());
    String beanName = "JSONRPCBridge" ;
    MyJSONRPCBridge bean = (MyJSONRPCBridge) webAppContext.getBean(beanName);
    session.setAttribute(beanName, bean);
    ...
    }
    Last edited by cyrille37; May 21st, 2007 at 03:40 AM. Reason: add more code example

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    From your description I can't tell you much more than SessionScope DOES put the beans into the session. Where do you have problems to access it?

    Jörg

  3. #3
    Join Date
    Jul 2006
    Posts
    22

    Default

    thanks for your answer.

    But if I do not add my HttpSessionListener,
    in the service(HttpServletRequest request, HttpServletResponse response) method of the HttpServlet JSONRPCServlet
    a call to session.getAttribute("JSONRPCBridge") return null
    whereas the bean JSONRPCBridge is declared as a session bean.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    So are you asking how to eagerly load the bean? e.g. instead of manually asking for it with getBean(..), how to ensure a Session scoped bean is created when a new session is created.
    Last edited by karldmoore; Aug 30th, 2007 at 07:29 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5
    Join Date
    Jul 2006
    Posts
    22

    Smile

    Quote Originally Posted by karldmoore View Post
    So are you asking how to eagerly load the bean? e.g. instead of manually asking for it with getBean(..), how to ensure a Session scoped bean is created when a new session is created.
    Yes, this is a best rewriting of my question ! thanks !

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Karl seems to be good in guessing people's needs

    That means your MySessionListener also works (i.e. meets your requirements by eagerly loading the bean and storing it in the session) if you don't put the bean into the session yourself, i.e. when you remove session.setAttribute(beanName, bean)?

    Why do you need the bean be loaded eagerly? If that's really a requirement I'd even say your HttpSessionListener approach is appropriate.

    Jörg

  7. #7
    Join Date
    Jul 2006
    Posts
    22

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    Karl seems to be good in guessing people's needs
    A great spirit

    Quote Originally Posted by Jörg Heinicke View Post
    Why do you need the bean be loaded eagerly? If that's really a requirement I'd even say your HttpSessionListener approach is appropriate.
    In the case of using the session bean in a web page with Tapestry, all thinhs are allright.
    But I need to accept a first web call done with JSON-RPC (http://oss.metaparadigm.com/jsonrpc/) from a Java Applet. JSON-RPC is running with a servlet, and that servlet needs to get a object in the session. Then the same client will use some web page with the same session. It's why I need the bean to be eagerly loaded.

    cyrille.

  8. #8
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    Karl seems to be good in guessing people's needs
    Hehe, is that such a good thing . It looks good when you get it right, you just have to forget about all the failed attempts .
    Last edited by karldmoore; Aug 30th, 2007 at 07:29 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •