0 Replies Latest reply on Dec 20, 2006 6:54 PM by weiming chen

    Display data via EJB 3

    weiming chen Newbie

      Hi there

      I can create, insert and delete date via EJB 3, but I got a problem with displaying data.

      I created an entitybean "UserCredentialsEntity.java", which defined all setter/getter.

      In interface "UserCredentialsHandler.java", I set a method
      "public Collection getUser(String firstName);"

      In Javabean "UserCredentialsBean.java", I implemented this method
      "public Collection getUser(String firstName){
      String oql = "select u from UserCredentialsEntity u where u.firstName=:firstName";

      return em.createQuery(oql).setParameter("firstName", firstName).getResultList();
      }"

      Then I create a test.jsp to test this method: (code about displaying data)

      <%@page import="javax.naming.*,
      java.text.*,
      java.util.*,
      com.SurveyMaker.ejb.entity.*,
      com.SurveyMaker.ejb.session.*"%>

      <%!
      private UserCredentialsHandler aUser = null;

      public void jspInit () {
      try {
      InitialContext ctx = new InitialContext();
      aUser = (UserCredentialsHandler) ctx.lookup("surveymaker/UserCredentialsBean/local");
      } catch (Exception e) {
      e.printStackTrace ();
      }
      }%>

      <%
      Collection somePeople= aUser.getUser("Allen");
      for(Iterator iter = somePeople.iterator(); iter.hasNext();){
      UserCredentialsEntity user = (UserCredentialsEntity)iter.next();
      %>
      Username: <%=user.getFirstName() %>
      <%
      }
      %>

      It brings me an error:

      org.apache.jasper.JasperException: Exception in JSP: /test.jsp:29

      26: <%
      27: Collection somePeople= aUser.getUser("Allen");
      28: for(Iterator iter = somePeople.iterator(); iter.hasNext();){
      29: UserCredentialsEntity user = (UserCredentialsEntity)iter.next();
      30: %>
      31: Username: <%=user.getFirstName() %>
      32: <%


      Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      root cause

      java.lang.ClassCastException: com.SurveyMaker.ejb.entity.UserCredentialsEntity
      org.apache.jsp.test_jsp._jspService(test_jsp.java:71)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      Can anyone help me, please?

      Cheers
      Ming