Witam,
tworzę aplikację webową (serwis ogłoszeniowy) w oparciu o JPA, EJB, JSP i Servlety.
Mam dylemat, w jaki sposób pobierać dane z bazy. Do tej pory przetestowałem kilka wariantów - każdy działa, ale nie wiem który jest najbardziej "elegancki".
Wstrzykiwane beany są typu Stateless.
Rozwiązanie 1 - Servlet + JSP
Servlet ListUsersServlet
...
@EJB
protected UserDTOFacadeLocal userDao;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<UserDTO> users = userDao.findAll();
request.setAttribute("userList", users);
request.getRequestDispatcher("ListUsers.jsp").forward(request, response);
}
...
ListUsers.jsp
...
<table id="userListTable" border="3">
<tr >
<th bgcolor=>ID</th>
<th bgcolor=>FirstName</th>
<th bgcolor=>LastName</th>
<th bgcolor=>Login</th>
<th bgcolor=>Password</th>
<th bgcolor=>Level</th>
<th bgcolor=>Since</th>
</tr>
<c:forEach var="user" begin="0" items="${requestScope.userList}">
<tr>
<td>${user.id}</td>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.login}</td>
<td>${user.password}</td>
<td>${user.level}</td>
<td>${user.since}</td>
</tr>
</c:forEach>
</table>
...
Rozwiązanie 2 - tylko JSP
ListUsers.jsp
...
<%
Context ctx = new InitialContext();
UserDTOFacadeLocal userDao = (UserDTOFacadeLocal) ctx.lookup("java:global/Ads-ear/Ads-ejb-3.0-SNAPSHOT/UserDTOFacade!dao.UserDTOFacadeLocal");
List<UserDTO> userList = userDao.findAll();
request.setAttribute("userList", userList);
%>
<table id="userListTable" border="3">
<tr >
<th bgcolor=>ID</th>
<th bgcolor=>FirstName</th>
<th bgcolor=>LastName</th>
<th bgcolor=>Login</th>
<th bgcolor=>Password</th>
<th bgcolor=>Level</th>
<th bgcolor=>Since</th>
</tr>
<c:forEach var="user" begin="0" items="${requestScope.userList}">
<tr>
<td>${user.id}</td>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.login}</td>
<td>${user.password}</td>
<td>${user.level}</td>
<td>${user.since}</td>
</tr>
</c:forEach>
</table>
...
Myślałem jeszcze nad wstrzyknięciem beana przy logowaniu użytkownika, wyciągnięciu z niego listy userów i umieszczenie jej w ServletContext lub w sesji, ale nie wiem czy to dobre rozwiązanie.
Proszę o nakierowanie, które (jeśli którekolwiek w ogóle) najbardziej nadaje się do tego celu.
Pozdrawiam.