2 Replies Latest reply on May 30, 2008 2:10 PM by mbilalf.mbilalf.gmail.com

    Non JSF request. Injected hibernateSession is NULL.

    mbilalf.mbilalf.gmail.com

      Hi,
      I am using jboss-seam-2.0.1.GA with JDK1.6 deployed in jboss-4.2.2.GA


      I have a https client that interacts with a servlet in seam based application. It is a simple servlet that is deployed along with seam.


      Servlet calls a stateless session bean for required data.The hibernateSession injected by seam is available in one request but is null in other.


      I wonder if request to a servlet other than FacesServlet has access to the seam components. If one request has the injected session the other should also get it


      Here is the code




      Servlet


      public class TransportServlet extends HttpServlet {
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           if(reqType.equals(REQUEST_TYPE_A)) {
                handleRequestA();
           }
           else if(reqType.equals(REQUEST_TYPE_B)) {
                handleRequestB();
           }
      



      web.xml


          ...
          <filter>
              <filter-name>Seam Filter</filter-name>
              <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
          </filter>
      <filter-mapping>
              <filter-name>Seam Filter</filter-name>
              <url-pattern>*.seam</url-pattern>
          </filter-mapping>
          ...
           <servlet>
              <servlet-name>TransportServlet</servlet-name>
              <servlet-class>com.server.https.TransportServlet</servlet-class>
           </servlet>
           <servlet-mapping>
               <servlet-name>TransportServlet</servlet-name>
               <url-pattern>/servlet/TransportServlet</url-pattern>
           </servlet-mapping>
      



      Component.xml


      <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
          <persistence:managed-hibernate-session name="hibernateSession" 
                         auto-create="true"
                                  session-factory-jndi-name="java:/sessionFactory"
                             />




      Handler method calls a stateless session bean to get the required data. Session bean uses a DBService class that does hibernate request to the database.
      Hibernate session is injected into the DBService class.


      DBService class


      @Name("locationService")
      public class LocationService {
      
          @In(create=true,required=true) Session hibernateSession;
          
          public List getPrinters(String locationId){
              ...
              hibernateSession.createQuery(hsqlStr);
              ...
          }
      
          public Location getLocation(String locationId){
              ...
              hibernateSession.createQuery(hsqlStr);
              ...
          }
      }



      hibernateSession is injected fine when getPrinters() is called. But is found null in case of getLocation()



      Can anyone help me out here!!
      thanks in advance.

        • 1. Re: Non JSF request. Injected hibernateSession is NULL.
          nickarls

          Tried ContextualHttpServletRequest and accessing the components through Components?

          • 2. Re: Non JSF request. Injected hibernateSession is NULL.
            mbilalf.mbilalf.gmail.com

            Hi,


            I tried it, but got another problem. The data is not being read from the request now.
            My servlet with  ContextualHttpServletRequest is like this



            public void doPost( final HttpServletRequest request, final HttpServletResponse response) throws ServletException{
            
            new ContextualHttpServletRequest() {
               @Override
               public void process() {
            
                 HttpSession session = request.getSession();
            
                 ServerResponse res = null;
                 String reqType = request.getHeader(ServerDefinitions.REQUEST_TYPE);
                 System.out.println("----------- reqType: "+reqType);
                 String contentLen = request.getHeader(ServerDefinitions.CONTENT_LENGTH);
                 System.out.println("Request content Length :" + contentLen);
            
                 String sessID = request.getHeader(ServerDefinitions.SESSION_ID);
            
                 is = request.getInputStream();
                 byte[] by = new byte[BUFFER_SIZE];
                 int noOfBytes = 0;
                 StringBuffer buf = new StringBuffer();
                 bout = new ByteArrayOutputStream();
                 while ((noOfBytes = is.read(by)) != -1) {
                     bout.write(by, 0 , noOfBytes);
                     by = bout.toByteArray();
                     buf.append(new String(by,     
                        ServerDefinitions.STRING_ENCODING_UTF_8));
                     bout = new ByteArrayOutputStream();
                     by = new byte[BUFFER_SIZE];
                 }
                 String data = buf.toString();
            
                 System.out.println("$$$ RequestData :- "+data);
                 if (reqType.equals(RequestTypes.A)) {
                 ...
                 }else if(reqType.equals(RequestTypes.B)) {
                 ...
                 }
               }
            }.run();
            }
            



            Another thought. Does SeamContext initialize when we request a simple servlet ?


            Best Regards,
            thanks for your time.