• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access EntityBean data from JSP page???  RSS feed

 
Arto Pastinen
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
My Web-Tier application is running next way:
(request) -> Controller -> performAction -> performModel -> include JSP page.
(The model comes from MVC-pattern)
Controller handles all requests, and perform action (usually some form submit, like adduser).
Then Controller performs a model class, where i get all the data, which i will show in JSP page.
The problem is that if i get data this way:
class AdminUsers {
/**
* PageContext comes from JSP page:
* <% ModelUtil.performModel(pageContext, "AdminUsers"); %>
*/
public void performModel(PageContext ctx) {
try{
UserTransaction t = getUserTransaction();
try{
t.begin();
CompanyLocalHome home = CompanyUtil.getLocalHome();
CompanyLocal company = home.findByPrimaryKey(ctx.getRequest().getAttribute("company_id"));
ctx.getRequest().setAttribute("company_users", company.getRoles().iterator()); // getRoles returns collection
t.commit();
}
catch(Exception e1) {
e1.printStackTrace();
t.rollback();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
.. and use it in JSP page next way:
<td>
Select user:
</td>
<td>
<select name="companyUsers">
<%
Iterator i = (Iterator) request.getAttribute("company_users");
UserLocal user;
while(i.hasNext()) {
user = (UserLocal) i.next();
%>
<option value="<%=user.getUserId()%>"><%=user.getUserName()%></option>
<%}%>
</td>
.. i get an IllegalStateException, or something that kind an exception, because the transaction has ended.
So, what is a correct way to display data in JSP page, which has get from EntityBean, and can i user DAO here??
(i don't know anything about DAO pattern.. is there any reasons to study them??)
i can't use UserTransaction around include JSP page:
UserTransaction t;
t.begin();
includeJsp("AdminUsers.jsp");
t.commit();
.. because i will include other jsp's in my jsp, recursively, an there will be same code,
and portability will suffer from these nested transactions. (my jboss don't support them..)
hm.. i think that there was all necessery information, btw. sorry my bad english, and thanks for all who can help me!!
Artsi
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much better answer -- DO NOT use Entity beans from a JSP! That is a LOUSY coding practice -- not only is it difficult, but abominably slow! Go download the Sun J2EE blueprints and learn about Session Facades and Value objects -- THAT is the way you should be handling data displayed in your JSP.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!