File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Populating HTML table from data query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Populating HTML table from data query" Watch "Populating HTML table from data query" New topic
Author

Populating HTML table from data query

Martin Baldwin
Greenhorn

Joined: Jun 21, 2013
Posts: 8
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2416
    
    7

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.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Martin Baldwin
Greenhorn

Joined: Jun 21, 2013
Posts: 8
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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

Joined: Jun 21, 2013
Posts: 8
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

Joined: Feb 26, 2013
Posts: 32
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

How is your servlet creating the logs scoped variable?
Martin Baldwin
Greenhorn

Joined: Jun 21, 2013
Posts: 8
I'm not entirely sure. I thought that I could treat the logs entity as a collection to iterate over.
Marshall Blythe
Ranch Hand

Joined: Feb 26, 2013
Posts: 32
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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

Joined: Jun 21, 2013
Posts: 8
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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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

Joined: Jan 10, 2002
Posts: 61217
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating HTML table from data query