• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating HTML table from data query

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic