1 Reply Latest reply on Apr 7, 2006 1:28 PM by Vijay G

    EJB3.0 OBJECT cannot be acquired in JSP.

    yuka shimizu Newbie

      I am acquiring data from db by calling the SessionBean from the Servlet, and using EntityManager.
      It is not possible to achieve it well though it wants to return Servlet the data of
      the EntityBean type acquired in the SessionBean, and to pass it to JSP.
      It is displayed in the log of jboss, "INFO [STDOUT] null".
      The character of String Type can be normally passed.
      It was similar though not only EntityBean but also the DTO object was executed making it.

      Am I doing something mistake?
      could anyone have advice for me?


      JSP

      <html>
       <head><title>EJB Test</title></head>
       <BODY bgcolor="#ffffff">
      <%
       Value v = new Value();
       v = (Value)request.getAttribute("__value");
       ListIterator userlist = (ListIterator)v.getAttribute("userlist");
      %>
       <table width="400" border="0" cellpadding="0" cellspacing="0">
       <tr>
       <td valign="top">
       <div align="center">
       Result
       <table width="400" border="1" cellspacing="1" width="100%" cellpadding="5" height="62">
       <tr>
       <td align="right" width="100" height="30">ID</td>
       <td height="30">passwd</td>
       <td height="30">name</td>
       </tr>
      <%
       while(userlist.hasNext()){
       UserBean user = new UserBean();
       user = (UserBean)userlist.next();
      %>
       <tr>
       <td align="right" height="30"><%=user.getUserId()%></td>
       <td height="30"><%=user.getPasswd()%></td>
       <td height="30"><%=user.getName()%></td>
       </tr>
      <%
       }
      %>
       </table>
       </div>
       </td>
       </tr>
       </table>
       </BODY>
      </html>


      View Controler Servlet Class
      public class MHServlet extends HttpServlet {
       public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
       try{
       String event = "";
       event = new String(req.getParameter("__magazine").getBytes("8859_1"), "EUC-JP");
      
       Hashtable param = new Hashtable();
       for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
       String key = (String)e.nextElement();
       param.put(key, new String(req.getParameter(key).getBytes("8859_1"), "EUC-JP"));
       }
      
       Value value = null;
       Context context = new InitialContext();
       if(event.equals("MH006_001")){
       MH006_001 c = (MH006_001)context.lookup(MH006_001.class.getName());
       value = c.doBiz(param); }
       forward(value, req, res);
      
       }catch(Exception e){
       System.out.println(e.getMessage());
       }
       }
       private void forward(Value value, HttpServletRequest req, HttpServletResponse res){
       req.setAttribute("__value", value);
       RequestDispatcher rd = getServletContext().getRequestDispatcher(value.getForwardPage());
      
       try{
       rd.forward(req, res);
       }catch(ServletException e){
       puterror(e.getMessage(), res);
       }catch(IOException e){
       puterror(e.getMessage(), res);
       }
       }
      }


      SessionBean Class
      @Stateful
      @javax.ejb.Remote(MH006_001.class)
      
      public class MH006_001Bean implements MH006_001, Serializable{
       private String searchId;
       @PersistenceContext//(unitName="ejb3mn")
       protected EntityManager em;
      
       public Value doBiz(Hashtable param){
       String page = "/jsp/MH007.jsp";
       int count = 0;
       Value value = new Value();
       try{
       searchId = (String)param.get("searchId");
      
       Query query = em.createQuery("select OBJECT(o) from UserBean o where o.userId like :userId");
       query.setParameter("userId", "%"+ searchId + "%");
       List userlist = query.getResultList();
       count = userlist.size();
      
       LinkedList ret = new LinkedList();
       for(int i=0; i<count; i++){
       UserBean user = (UserBean)userlist.get(i);
       ret.add(user);
       }
       value.setAttribute("userlist", ret.listIterator(0));
       value.setForwardPage(page);
      
       }catch(Exception ex){
       System.out.println("***** MH006_001Bean.doBiz catch ex = " + ex);
       value.setForwardPage("/jsp/Error.jsp");
       }
       return value;
       }
      }


      Class returned to servlet(It is in the same package as bean.)
      public class Value implements Serializable{
       private String nextPage = null;
       private Hashtable value = null;
      
       public Value(){
       value = new Hashtable();
       }
      
       public void setForwardPage(String page){
       nextPage = page;
       }
      
       public String getForwardPage(){
       return nextPage;
       }
      
       public void setAttribute(Object key, Object data){
       value.put(key, data);
       }
      
       public Object getAttribute(Object key){
       return value.get(key);
       }
      }