Announcement Announcement Module
Collapse
No announcement yet.
session bean and other servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 03:40 AM. Reason: add more code example

  • #2
    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

    Comment


    • #3
      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.

      Comment


      • #4
        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, 07:29 AM.

        Comment


        • #5
          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 !

          Comment


          • #6
            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

            Comment


            • #7
              Originally posted by Jörg Heinicke View Post
              Karl seems to be good in guessing people's needs
              A great spirit

              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.

              Comment


              • #8
                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, 07:29 AM.

                Comment

                Working...
                X