9 Replies Latest reply on Oct 15, 2006 6:23 AM by Umar Zubair

    How can I create seam context etc in httpservlet

    Umar Zubair Newbie

      Hi,

      I am using Seam framework.
      Now i have to implement a servlet which will get a XML data file as a request. I have to import data within that file. How can i get/create seam contexts (hibernate session, logged user etc) within that servlet. Right now i am using HttpServlet.

      Regards,
      Umar

        • 2. Re: How can I create seam context etc in httpservlet
          Sebastián Fiorentini Newbie

          No, SeamServletFilter is intended to create an process Ajax calls.
          I've implemented a Filter which does some things, and uses Seam components.

          Inside my Filter class I have the following method which gets called in every request.

          protected void inicarSeam(ServletRequest request, ServletResponse response) {
          HttpSession session = ((HttpServletRequest) request).getSession(true);
          Lifecycle.setPhaseId(PhaseId.INVOKE_APPLICATION);
          Lifecycle.setServletRequest(request);
          Lifecycle.beginRequest(servletContext, session, request);
          Manager.instance().restoreConversation(request.getParameterMap());
          Lifecycle.resumeConversation(session);
          Manager.instance().handleConversationPropagation(
          request.getParameterMap());
          }

          And in my doFilter() method:

          String userName = httpRequest.getUserPrincipal().getName();
          inicarSeam(request, response);
          // Si llego acá es porque el contenedor nos autenticó
          Usuario usuario = (Usuario) Component.getInstance(Usuario.class, false);

          • 3. Re: How can I create seam context etc in httpservlet
            Sebastián Fiorentini Newbie

            No, SeamServletFilter is intended to create an process Ajax calls.
            I've implemented a Filter which does some things, and uses Seam components.

            Inside my Filter class I have the following method which gets called in every request.

            protected void inicarSeam(ServletRequest request, ServletResponse response) {
            HttpSession session = ((HttpServletRequest) request).getSession(true);
            Lifecycle.setPhaseId(PhaseId.INVOKE_APPLICATION);
            Lifecycle.setServletRequest(request);
            Lifecycle.beginRequest(servletContext, session, request);
            Manager.instance().restoreConversation(request.getParameterMap());
            Lifecycle.resumeConversation(session);
            Manager.instance().handleConversationPropagation(
            request.getParameterMap());
            }

            And in my doFilter() method:

            String userName = httpRequest.getUserPrincipal().getName();
            inicarSeam(request, response);
            Usuario usuario = (Usuario) Component.getInstance(Usuario.class, false);

            • 4. Re: How can I create seam context etc in httpservlet
              Gavin King Master

              SeamServletFilter is the right way to go. Its not *only* for AJAX.

              • 5. Re: How can I create seam context etc in httpservlet
                Umar Zubair Newbie

                Now, I am using SeamServletFilter.

                I have to use following line of code in my httpservlet

                AuthenticationBean authBean = new AuthenticationBean();
                authBean.setLoginId(userId);
                authBean.setPassword(pwd);
                LoginAction la = (LoginAction) Component.getInstance(LoginAction.class);
                org.jboss.seam.contexts.Contexts.getSessionContext().set("authBean",authBean);
                la.login();

                It is working.

                What I want to know is whether it is a recommended way to initiate beans and components or there is a better way to do it.

                Thanks for the hint.

                Umar

                • 6. Re: How can I create seam context etc in httpservlet
                  Gavin King Master

                  This is the right way.

                  • 7. Re: How can I create seam context etc in httpservlet
                    Umar Zubair Newbie

                    Ok.

                    Now I have a couple of more issues.

                    1: How can I create FacesContext within my httpservlet. I am using FacesContext for getting ResourceBundle and Locale etc. For component calls from JSF FacesContext.getCurrentInstance() is working fine.

                    2: Database changes are not being commited (calling components from httpservlet). Select statements are working fine. If i call same methods from JSF pages it is working fine.

                    Thanks for your prompt replies.

                    Umar

                    • 8. Re: How can I create seam context etc in httpservlet
                      Serg Prasolov Newbie

                      1: I do like below:

                      FacesContextFactory _facesContextFactory =
                       (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
                      LifecycleFactory lifecycleFactory =
                       (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
                      ServletContext servletContext = getServletConfig().getServletContext();
                      String lifecycleId = servletContext.getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
                      javax.faces.lifecycle.Lifecycle _lifecycle = lifecycleFactory.getLifecycle(
                       lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE
                      );
                      FacesContext facesContext =
                       _facesContextFactory.getFacesContext(
                       servletContext,
                       request,
                       response,
                       _lifecycle
                       );


                      BTW, isn't it too complex?


                      • 9. Re: How can I create seam context etc in httpservlet
                        Umar Zubair Newbie

                        Now insert/updates are working.
                        I have done two changes.
                        1. At start of httpservlet, I have written lines of code
                        // to start transaction, it is required for any update, insert
                        try {
                        if (!Transactions.isTransactionActiveOrMarkedRollback()) {
                        //log.debug("beginning transaction prior to phase: " + phaseId);
                        Transactions.getUserTransaction().begin();
                        }
                        }
                        catch (Exception e) {
                        throw new IllegalStateException("Could not start transaction", e);
                        }

                        2. At end of servlet, I have used following line of code
                        // it is required to commit or rollback transaction

                        try {
                        if (Transactions.isTransactionActive()) {
                        Transactions.getUserTransaction().commit();
                        }
                        else if (Transactions.isTransactionMarkedRollback()) {
                        // log.debug("rolling back transaction after phase: " + phaseId);
                        Transactions.getUserTransaction().rollback();
                        }
                        }
                        catch (Exception e) {
                        throw new IllegalStateException("Could not commit transaction", e);
                        }


                        Umar