0 Replies Latest reply on Jun 26, 2008 1:56 AM by leon qian

    communication from httpsession with jsf, please give me a hand

    leon qian Newbie


      hi man, i am making a hits count, i want to insert begintime into database when a session created. and insert into endtime when it destoryed.

      i have puzzled by communication from httpsession with jsf.
      i think it is diffcult to say hello to jsf from httpsession code. but any solution can result?
      my code belows:


      
      //OnlineCounterListener.java
      
      
      public class OnlineCounterListener implements HttpSessionListener {
      
              private MyTestFinder myTestFinder;
      
              public void sessionCreated(HttpSessionEvent hse) {
      
                      
      
           myTestFinder = (MyTestFinder) Component.getInstance("myTestFinder");
      
                if(myTestFinder!=null)
      
                     myTestFinder.createMyTest();
      
                System.out.println("--------------------------hello jsf i love you!");
      
      }
      
               
      
      public void sessionDestroyed(HttpSessionEvent hse) {
      
                      
      
              }
      
      
      }
      
      
      
      //web.xml
      
      <?xml version="1.0" encoding="UTF-8"?>
      
      
      <web-app version="2.5"
      
          xmlns="http://java.sun.com/xml/ns/javaee"
      
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      
      
          <!-- Seam -->
      
          
      
          <listener>
      
              <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
      
          </listener>
      
      
          <filter>
      
              <filter-name>Seam Exception Filter</filter-name>
      
              <filter-class>org.jboss.seam.servlet.SeamExceptionFilter</filter-class>
      
          </filter>
      
      
          <filter-mapping>
      
              <filter-name>Seam Exception Filter</filter-name>
      
              <url-pattern>*.jsf</url-pattern>
      
          </filter-mapping>
      
      
          <!-- MyFaces -->
      
          
      
          <listener>
      
              <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
      
          </listener>
      
          
      
          <context-param>
      
              <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
      
              <param-value>client</param-value>
      
             
      
          </context-param>
      
      
           <servlet>
      
                <servlet-name>Faces Servlet</servlet-name>
      
                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
      
                <load-on-startup>1</load-on-startup>
      
           </servlet>
      
      
           <servlet-mapping>
      
                <servlet-name>Faces Servlet</servlet-name>
      
                <url-pattern>*.jsf</url-pattern>
      
           </servlet-mapping>
      
           
      
           
      
           <!--ajax-->
      
           
      
            
      
           <display-name>a4jEchoText</display-name>
      
      
          <filter> 
      
                <display-name>Ajax4jsf Filter</display-name> 
      
                <filter-name>ajax4jsf</filter-name> 
      
                <filter-class>org.ajax4jsf.Filter</filter-class> 
      
           </filter> 
      
           
      
           <filter-mapping> 
      
               <filter-name>ajax4jsf</filter-name> 
      
                <servlet-name>Faces Servlet</servlet-name>
      
                <dispatcher>REQUEST</dispatcher>
      
                <dispatcher>FORWARD</dispatcher>
      
                <dispatcher>INCLUDE</dispatcher>
      
           </filter-mapping>
      
           
      
         
      
          <filter-mapping>     
      
            <filter-name>ajax4jsf</filter-name>     
      
            <url-pattern>*.seam</url-pattern>   
      
          </filter-mapping>   
      
          <!--
      
          <context-param>     
      
            <param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>     
      
            <param-value>org.jboss.seam.ui.facelet.SeamFaceletViewHandler</param-value>   
      
          </context-param>  
      
      
           <context-param>
      
              <param-name>org.ajax4jsf.SKIN</param-name>
      
              
      
                <param-value>#{skinBean.skin}</param-value>
      
           </context-param>
      
          -->
      
           <filter>
      
                <filter-name>UploadBeanFilter</filter-name>
      
                <display-name>UploadBeanFilter</display-name>
      
                <filter-class>javazoom.upload.filter.UploadBeanFilter</filter-class>
      
                <init-param>
      
                     <param-name>encoding</param-name>
      
                     <param-value>ISO-8859-1</param-value>
      
                </init-param>
      
                <init-param>
      
                     <param-name>parser</param-name>
      
                     <param-value>CFUPARSER</param-value>
      
                </init-param>
      
                <init-param>
      
                     <param-name>maxcontentlength</param-name>
      
                     <param-value>1073741824</param-value>
      
                </init-param>
      
           </filter>
      
           <filter-mapping>
      
                <filter-name>UploadBeanFilter</filter-name>
      
                <url-pattern>/*</url-pattern>
      
           </filter-mapping>
      
           
      
      
           <session-config>
      
              <session-timeout>1</session-timeout> 
      
          </session-config>   
      
         
      
          <listener> 
      
            <listener-class> 
      
              com.CMS.hitscount.OnlineCounterListener 
      
            </listener-class> 
      
          </listener> 
      
          <listener>
      
            <listener-class>
      
               com.CMS.hitscount.ApplicationWatch
      
            </listener-class>
      
          </listener>
      
      
      
      </web-app>
      
      
      //MyTest.java
      
      @Entity
      
      @Table(name = "mytest")
      
      public class MyTest implements Serializable {
      
           long Id;
      
           
      
           
      
           String testVaule;
      
           
      
           
      
           @Id
      
           @GeneratedValue
      
           @Column(name = "ID")
      
           public long getId() {
      
                return Id;
      
           }
      
           public void setId(long id) {
      
                this.Id = id;
      
           }
      
      
           @Column(name = "TestVaule", length = 200)
      
           public String getTestVaule() {
      
                return testVaule;
      
           }
      
           public void setTestVaule(String testVaule) {
      
                this.testVaule = testVaule;
      
           }
      
           
      
      }
      
      
      //MyTestFinder.java
      
      @Local
      
      public interface MyTestFinder {
      
           
      
           public void createMyTest();
      
           public void updateMyTest();
      
           public String getHtml();
      
           public String findMyTest();
      
           public void destroy();
      
      }
      
      
      
      //MyTestFinderBean.java
      
      
      @Stateful
      
      @Name("myTestFinder")
      
      
      public class MyTestFinderBean implements MyTestFinder, Serializable {
      
      
           @PersistenceContext
      
           private EntityManager entityManager;
      
           
      
           @Valid
      
           private MyTest myTest = new MyTest();
      
      
           @Logger
      
           private static Log log;
      
      
           @DataModel
      
           private List<MyTest> myTestList;
      
           
      
           private String html="dd";
      
      
      
           
      
           @In(create=true)
      
           private MyTestEditor myTestEditor;
      
           
      
           public String getHtml() {
      
                
      
                return this.html;
      
           }
      
           public void createMyTest()
      
           {
      
                myTestEditor.create();
      
           }
      
           public void updateMyTest()
      
           {
      
                myTestEditor.update();
      
           }
      
           @Factory("myTestList")
      
           @Begin(join = true)
      
           public String findMyTest(){
      
                executeQuery();
      
                return "mytest";
      
           }
      
           private void executeQuery() {
      
                
      
                List<MyTest> items = entityManager.createQuery(
      
                          "select c from MyTest'").getResultList();
      
      
                
      
                
      
                if (items.size() > 0) {
      
                
      
                     myTestList = items;
      
                }
      
                
      
           }
      
           @Destroy
      
           @Remove
      
           public void destroy() {
      
           }
      
      
      }
      
      
      //MyTestEditor.java
      
      @Local
      
      public interface MyTestEditor {
      
           
      
           public MyTest getInstance();
      
      
           public void setInstance(MyTest instance);
      
      
           public String update();
      
      
           public String create();
      
           
      
           public String getHtml();
      
           
      
           public void destroy();
      
      }
      
      //MyTestEditorBean.java
      
      
      @Name("myTestEditor")
      
      @Stateful
      
      public class MyTestEditorBean implements MyTestEditor, Serializable {
      
      
           @In(create = true)
      
           private EntityManager entityManager;
      
      
           @Valid
      
           private MyTest myTest = new MyTest();
      
      
           @Logger
      
           private static Log log;
      
      
           public MyTest getInstance() {
      
                return myTest;
      
           }
      
      
           public void setInstance(MyTest instance) {
      
                this.myTest = instance;
      
           }
      
      
           // @Restrict("#{identity.loggedIn}")
      
           @Begin(join = true)
      
           public String create() {
      
                myTest = new MyTest();
      
                SimpleDateFormat bartDateFormat =     new SimpleDateFormat("EEEE-MMMM-dd-yyyy");        
      
                Date date = new Date();       
      
                System.out.println(bartDateFormat.format(date));
      
                myTest.setTestVaule(bartDateFormat.format(date));
      
                entityManager.persist(myTest);
      
      
                return "test";
      
      
           }
      
      
           public String update() {
      
      
                if (myTest != null) {
      
                     myTest.setTestVaule("what!!!");
      
                     entityManager.merge(myTest);
      
                     // entityManager.flush();
      
                     System.out.println("update over");
      
                }
      
      
                return "test";
      
           }
      
      
           private String html = "dd";
      
      
           public String getHtml() {
      
                return this.html;
      
           }
      
      
           @Destroy
      
           @Remove
      
           public void destroy() {
      
      
           }
      
      
      }
      
      
      


      my error messages:



      
      2008-06-26 07:48:48,343 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,343 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,343 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,343 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,343 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,343 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.postDestroyContext.CONVERSATION
      
      2008-06-26 07:48:48,343 INFO  [STDOUT] 1
      
      2008-06-26 07:48:48,343 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/cms]] Session event listener threw exception
      
      java.lang.IllegalStateException: No application context active
      
           at org.jboss.seam.Component.forName(Component.java:1577)
      
           at org.jboss.seam.Component.getInstance(Component.java:1627)
      
           at org.jboss.seam.Component.getInstance(Component.java:1610)
      
           at org.jboss.seam.Component.getInstance(Component.java:1604)
      
           at com.CMS.hitscount.OnlineCounterListener.sessionCreated(Unknown Source)
      
           at org.apache.catalina.session.StandardSession.tellNew(StandardSession.java:384)
      
           at org.apache.catalina.session.StandardSession.setId(StandardSession.java:356)
      
           at org.apache.catalina.session.ManagerBase.createSession(ManagerBase.java:825)
      
           at org.apache.catalina.session.StandardManager.createSession(StandardManager.java:290)
      
           at org.apache.catalina.connector.Request.doGetSession(Request.java:2223)
      
           at org.apache.catalina.connector.Request.getSession(Request.java:2024)
      
           at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:831)
      
           at org.apache.myfaces.context.servlet.ServletExternalContextImpl.getSession(ServletExternalContextImpl.java:197)
      
           at org.jboss.seam.contexts.ContextAdaptor.getSession(ContextAdaptor.java:42)
      
           at org.jboss.seam.contexts.Lifecycle.beginRequest(Lifecycle.java:42)
      
           at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:52)
      
           at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersBefore(PhaseListenerManager.java:70)
      
           at org.apache.myfaces.lifecycle.LifecycleImpl.restoreView(LifecycleImpl.java:103)
      
           at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:66)
      
           at javax.faces.webapp.FacesServlet.service(FacesServlet.java:137)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      
           at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:127)
      
           at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:277)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      
           at javazoom.upload.filter.UploadBeanFilter.doFilter(UploadBeanFilter.java:95)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      
           at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
      
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
      
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
      
           at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
      
           at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      
           at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
      
           at java.lang.Thread.run(Thread.java:595)
      
      2008-06-26 07:48:48,359 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,359 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,359 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,359 DEBUG [org.jboss.seam.core.Events] Processing event:org.jboss.seam.beforePhase
      
      2008-06-26 07:48:48,375 DEBUG [org.jboss.seam.jsf.SeamVariableResolver] resolving name: ajaxContext
      
      2008-06-26 07:48:48,375 DEBUG [org.jboss.seam.Component] seam component not found: ajaxContext
      
      2008-06-26 07:48:48,375 DEBUG [org.jboss.seam.jsf.SeamVariableResolver] could not resolve name
      
      2008-06-26 07:48:48,390 DEBUG [org.jboss.mx.loading.RepositoryClassLoader] setRepository, repository=org.jboss.mx.loading.HeirarchicalLoaderRepository3@369fdc, cl=org.jboss.mx.loading.HeirarchicalLoaderRepository3$CacheClassLoader@8978c7{ url=null ,addedOrder=0}
      
      2008-06-26 07:48:48,421 DEBUG [org.jboss.mx.loading.RepositoryClassLoader] setRepository, repository=org.jboss.mx.loading.HeirarchicalLoaderRepository3@369fdc, cl=org.jboss.mx.loading.HeirarchicalLoaderRepository3$CacheClassLoader@d4f13a{ url=null ,addedOrder=0}
      
      2008-06-26 07:48:48,437 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.localeSelector
      
      2008-06-26 07:48:48,437 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.localeSelector
      
      2008-06-26 07:48:48,437 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.core.localeSelector
      
      2008-06-26 07:48:48,437 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.core.events
      
      2008-06-26 07:48:48,437 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.core.events