Rob Cime
Rob Cime

Reputation: 11

Display ArrayList (EJB session + Servlet + JSP, no JDBC)

I've search on stack and on the internet and I haven't find the anwser at my problem.

I try to display an ArrayList from ejb to a jsp page by a servlet. I use mysql-connector-java-5.1.22-bin and not jdbc. I succeed in getting data from the database but the servlet is unable to send this ArrayList to the jsp page. Can you help? I've tried many checks and it seems like the problem is when I get the arraylist in servlet.. I probably badly call it..

Here my code :

accessBean :

public class accessBean implements accesCatalogueBeanRemote,     accesCatalogueBeanLocal {
public List getLivresList() {
        String flightQuery = "SELECT p FROM produit p"; 
        Query q = em.createQuery(flightQuery); 
        List existing = q.getResultList();          
        return existing;
    }

}

My servlet :

@WebServlet("/servlet")
public class servlet_produit extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                HttpSession session = request.getSession(true);
                List list = null;

                //Connexion JNDI (annuaire pour localiser l'EJB)
                try{
                final Hashtable jndiProperties = new Hashtable();
                jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
                final Context context = new InitialContext(jndiProperties);
                final String appName = "BktEAR";
                final String moduleName = "Commands";

                final String beanName = "JNDI";
                final String viewClassName = accesCatalogueBeanRemote.class.getName();
                accesCatalogueBeanRemote remote = (accesCatalogueBeanRemote)
                context.lookup("ejb:"+appName+"/"+moduleName+"/"+
                beanName+"!"+viewClassName);
                list = remote.getLivresList();
                }
                catch (Exception e) {
                e.printStackTrace();
                }
                session.setAttribute("books", list);
                response.sendRedirect("product.jsp");
    }
}

And the place I call the servlet in my jsp page :

<c:forEach items="${books}" var="list">
    <tr>
        <td>ok : ${list.id}</td>
        <td><c:out value="${list.name}" /></td>
        <td><c:out value="${list.description}" /></td>
        <td><fmt:formatNumber value="${list.price}" type="currency" /></td>
    </tr>
</c:forEach>

Thanks by advance.

Upvotes: 1

Views: 163

Answers (1)

Mohsen
Mohsen

Reputation: 4643

You are putting data into your session in this line

session.setAttribute("books", list);

So you should retrieve them in jsp from session. You can access session in jsp by using ${sessionScope.books}.

This will helps you:

<c:forEach items="${sessionScope.books}" var="list">
    <tr>
        <td>ok : ${list.id}</td>
        <td><c:out value="${list.name}" /></td>
        <td><c:out value="${list.description}" /></td>
        <td><fmt:formatNumber value="${list.price}" type="currency" /></td>
    </tr>
</c:forEach>

Upvotes: 1

Related Questions