This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating HTML table from data query

 
Martin Baldwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

I am writing a google app engine app that stores individual logs and their properties as entities. It's very similar to the tutorial jsp code at the bottom of this page- https://developers.google.com/appengine/docs/java/gettingstarted/usingdatastore. Now I'm trying to output a single HTML table with values from my entity log with this:

Query query = new Query("Log", logkey)
List<Entity> logs = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(1000));

for (Entity log : logs) {
pageContext.setAttribute("user_content", log.getProperty("user"));
pageContext.setAttribute("age_content", log.getProperty("age"));
<h4>Age Logs:</h4>
<table border="1">
<tr>
<th>User</th>
<th>Age</th>
</tr>
<tr>
<td>${fn:escapeXml(user_content)}</td>
<td>${fn:escapeXml(age_content)}</td>

This displays an HTML table but individually for each log entry. How can I make it display a single table with all log entries? I've tried using the jstl c:forEach tag to solve this but haven't had any luck yet.

Thanks in advance,
Martin
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Let's see the complete and correct code for your use of <c:forEach>. Be sure to UseCodeTags when posting code.
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Currently it looks like you are trying to do everything in the JSP (having scriptlets) which is bad practice. Instead you should moving all those query, list generation, logic etc into a servlet then forward to the JSP and use JSTL.

 
Martin Baldwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was originally using c:forEach to store the attributes:



And yes, I realize using too many scriplets is bad, but I figured my application was simple enough to just do it all in JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Baldwin wrote:And yes, I realize using too many scriplets is bad

Using any scriptlets is bad. It's an obsolete technology from 12 years ago.

but I figured my application was simple enough to just do it all in JSP.

That's like saying "My circuit is simple enough to use vacuum tubes".

Even simple apps should use modern technology. Move the logic to the controllers and other classes, and limit your JSPs to the EL and JSTL.

After doing so, your list will be in request scope when the JSP is executed. In the JSP you'll use <c:forEach> to iterate over the data, creating a table row for each entry (assuming that's your aim).
 
Martin Baldwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I've got a new servlet set up that retrieves my data and stores it as new entity properties. The issue I'm having now is that I can't properly display these values using my JSP page. When I debug, the table shows up with no entries even though I have saved logs.


 
Marshall Blythe
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Baldwin wrote:


If the "logs" collection is null or empty then the body of this loop will be skipped and no table rows will be rendered. Have you verified that your servlet actually creates a non-empty collection and places it in a request/session attribute named "logs"?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is your servlet creating the logs scoped variable?
 
Martin Baldwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not entirely sure. I thought that I could treat the logs entity as a collection to iterate over.
 
Marshall Blythe
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Baldwin wrote:I'm not entirely sure. I thought that I could treat the logs entity as a collection to iterate over.


You can, but you must first create the collection, populate it, and assign it to a scoped variable named "logs" before the <c:forEach> tag can iterate over it. The best place to do all that is in the servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The JSP can't just snatch something out of thin air. You'll need to create the logs scoped variable in the page controller for the JSP.

If that's an unfamiliar concept, then this article might be helpful in explaining the structure of Java web apps.
 
Martin Baldwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that helps. So my page controller has the servlet mapped to the JSP through the URL /retrieve but how do I create the logs scoped variable? This is the line I use at the beginning of the table to call the servlet and the get method: and I'm currently using this for loop on my entity to set my desired properties:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Baldwin wrote:This is the line I use at the beginning of the table to call the servlet and the get method: <table border="1" form action="/retrieve" method="GET">

In no universe is that valid HTML.

In any case, you don't call a servlet from a JSP -- just the opposite. The page controller (a servlet) gets control first to gather any data needed by the JSP. It puts that data into the request scope as scoped variables (via setAttribute()), then forwards to the JSP. The JSP can then reference the scoped variables.

Please read the article I linked to. It explains all this in great detail.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (Entity log : logs) {
request.setAttribute("user", log.getProperty("user"));
request.setAttribute("date", log.getProperty("date"));

Why would you be creating scoped variables for each property?

You'd just put logs into request scope as a scoped variable, and iterate over it in the JSP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic