using if else statement for dynamic table jsp - java

EDIT:
I am retrieving an arraylist of data with data like:[category,content,category,content,...],and
I want to display a table on a JSP page with dynamic data like this:
<tr>
<td align="center">
Category of Tweets:
</td>
<td align="center">
All Tweets of User:
</td>
</tr>
<tr>
<td align="center">
Entertainment
</td>
<td align="center">
Tweet Content
</td>
</tr>
But using the source code I have below:
<table id="table" border="1" align="center">
<tr>
<td align="center">
Category of Tweets:
</td>
<td align="center">
All Tweets of User: <%out.println(userName); %>
</td>
</tr>
<%
for(int i=0;i<stringList.size();i++){
%>
<tr>
<td>
<%
if(i%2==0){
String category = stringList.get(i).toString();
out.print(category);
%>
</td>
<%}else{ %>
<td>
<%
String content = stringList.get(i).toString();
out.print(content);
}
%>
</td>
</tr>
<%
}
%>
</table>
The browser seems to duplicate extra table tags just before the else statement:
<td>
</td>
</tr>
<tr>
<td>
</td>
I am lost on how to resolve this.Could anybody tell me how should I amend the code?

Assuming what you said " I am retrieving an arraylist of data with data like:[category,content,category,content,...]" and that list contains pair elements you can iterate the list like presented below:
<table id="table" border="1" align="center">
<tr>
<td align="center">
Category of Tweets:
</td>
<td align="center">
All Tweets of User: <%out.println(userName); %>
</td>
</tr>
<%
for(int i=0;i<stringList.size()/2;i++){
%>
<tr>
<td align="center">
<%
String category = stringList.get(i*2).toString();
out.print(category);
%>
</td>
<td align="center">
<%
String content = stringList.get(i*2+1).toString();
out.print(content);
%>
</td>
</tr>
<%
}
%>
</table>
Note that category will iterate on elements situated in pair positions and 0 (0,2,4, ...) and content will iterate on elements situated in odd positions (1,3,5, ...)

You were ending <td> after you if condition and then starting a new <td> before the else.
Just try this one and see if it satisfies your expected output.
<td>
<%
if(i%2==0){
String category = stringList.get(i).toString();
out.print(category);
%>
</td>
<td>
<%
String content = stringList.get(i).toString();
out.print(content);
}
%>
</td>

check this code this might help you keep those tr and td tags in conditional statements
<table id="table" border="1" align="center">
<tr>
<td align="center">
Category of Tweets:
</td>
<td align="center">
All Tweets of User: <%out.println(userName); %>
</td>
</tr>
<%
for(int i=0;i<stringList.size();i++){
%>
<%
if(i%2==0){%><tr>
<td><%
String category = stringList.get(i).toString();
out.print(category);
%>
</td></tr>
<%}else{ %>
<tr>
<td>
<%
String content = stringList.get(i).toString();
out.print(content);%>
</td>
</tr><%
}
}
%>
</table>

How about
<td>
<%
if(i%2==0){
String category = stringList.get(i).toString();
out.print(category);
}
%>
</td>
<td>
<%
else{
String content = stringList.get(i).toString();
out.print(content);
}
%>
</td>

Related

sanitize a foreign entity when called in Java

I have a table that is generated from a database table. In that database table it has a foreign key linking it back to another database table. However when I print out the field it prints out the full path such as entity."" [""=""]. Is there a way to sanitize it so it will only return the result? I am trying to link to pages and things using the results number. Below in the tickets jsp page you will see I created a link on the sheetNum the directs you to viewBuyer?"buyerNum" however when you click the link instead of it going to say viewbuyer?1 it goes to viewBuyer?entity.BuyerInfo[ buyerNum=1 ]. I want to scrub out all the entity stuff and just be left with the value it is grabbing.
Servlet
case "/tickets":
try{
ticket = TicketDB.getTickets();
request.setAttribute("ticket", ticket);
} catch (NumberFormatException ex) {
ex.printStackTrace();
} break;
tickets.jsp
<div id="body">
<table id="buyerTable">
<c:forEach var="ticket" items="${ticket}" varStatus="iter">
<tr class="${((iter.index % 2) == 0) ? 'grey' : 'white'}">
<td>
${ticket.ticketId}
</td>
<td>
<a href="viewBuyer?${ticket.buyerNum}">
${ticket.sheetNum}
</a>
<br>
</td>
<td>
${ticket.lotNum}
</td>
<td>
${ticket.qty}
</td>
<td>
${ticket.price}
</td>
<td>
${ticket.description}
</td>
<td>
${ticket.buyerNum}
</td>
<td>
${ticket.paid}
</td>
<td>
${ticket.taxable}
</td>
<td>
${ticket.buyersPre}
</td>
<td>
${ticket.datePosted}
</td>
<td>
${ticket.dateUpdated}
</td>
</tr>
</c:forEach>
</table>
</div>

Closed Resultset: next error in jsp page, after upgrading to ojdbc7.jar from ojdbc14.jar via myEclipse

Creating two result sets in a Java class below & when return to the jsp page, access it below as such & then loop through the result sets to put data on the page fields.
Was working just fine with ojdbc14.jar, but now upgraded to ojdbc7.jar (for Oracle 12c) via myEclipse project. Getting a Closed Resultset: next error in the jsp page when access the first result set.
Any ideas or suggestions please with this upgrade being done?
I know I can use collections, etc., but trying to keep the code the same with this returned cursors result sets to be accessed in the jsp page. Thanks for any assistance.
jsp page:
<%
BCSData vBCS =
(BCSData)session.getAttribute("com.sherwin.barcodeshipping.bcsData");
%>
<TABLE class="data" >
<TR class="header">
<TD class="dataTxt"> Order Number </TD>
<TD class="dataTxt"> Rex </TD>
<TD class="dataTxt"> Size Code </TD>
<TD class="dataTxt"> Loc </TD>
<TD class="dataNbr"> Total Cartons </TD>
<TD class="dataNbr"> Total Pallets </TD>
<TD class="dataNbr"> Total Weight </TD>
<TD class="dataNbr"> Total Units </TD>
<TD class="dataNbr"> Order Units </TD>
<TD class="dataNbr"> Order Qty </TD>
</TR>
<%
while (vBCS.bolResultSet.next())
{
%>
<TR class="body">
<TD class="dataTxt"><%= vBCS.bolResultSet.getString(1) %> </TD>
<TD class="dataTxt"><%= vBCS.bolResultSet.getString(2) %> </TD>
<TD class="dataTxt"><%= vBCS.bolResultSet.getString(3) %> </TD>
<TD class="dataTxt"><%= vBCS.bolResultSet.getString(4) %> </TD>
<TD class="dataNbr"><%= vBCS.bolResultSet.getFloat(5) %> </TD>
<TD class="dataNbr"><%= vBCS.bolResultSet.getFloat(6) %> </TD>
<TD class="dataNbr"><%= vBCS.bolResultSet.getFloat(7) %> </TD>
<TD class="dataNbr"><%= vBCS.bolResultSet.getFloat(8) %> </TD>
<TD class="dataNbr"><%= vBCS.bolResultSet.getFloat(9) %> </TD>
<TD class="dataNbr"><%= vBCS.bolResultSet.getFloat(10) %> </TD>
</TR>
<%
}
%>
</TABLE>
<HR class="innerMed" >
<TABLE class="data">
<TR class="header">
<TD class="dataTxt"> WHS </TD>
<TD class="dataTxt"> Customer </TD>
<TD class="dataTxt"> Dock </TD>
<TD class="dataNbr"> Total Pallets </TD>
<TD class="dataNbr"> Total Weight </TD>
<TD class="dataNbr"> Total Units </TD>
</TR>
<%
while (vBCS.bolSummaryResultSet.next())
{
%>
<TR class="body">
<TD class="dataTxt"><%= vBCS.bolSummaryResultSet.getString(1)
%> </TD>
<TD class="dataTxt"><%= vBCS.bolSummaryResultSet.getString(2)
%> </TD>
<TD class="dataTxt"><%= vBCS.bolSummaryResultSet.getString(3)
%> </TD>
<TD class="dataNbr"><%= vBCS.bolSummaryResultSet.getFloat(4)
%> </TD>
<TD class="dataNbr"><%= vBCS.bolSummaryResultSet.getFloat(5)
%> </TD>
<TD class="dataNbr"><%= vBCS.bolSummaryResultSet.getFloat(6)
%> </TD>
</TR>
<%
} // while
%>
</TABLE>
Java class where the result sets were created and closed:
public ResultSet bolResultSet;
public ResultSet bolSummaryResultSet;
public Integer assignDock(String pDBSource, String pLoginID)
{
Integer vRetCode;
String vRetMsg;
Connection vConnection = null;
CallableStatement vCallStmt = null;
try
{
vConnection = DBConnection.getConnection(pDBSource, pLoginID);
//System.out.println("BCSData.java assignDock");
vCallStmt = vConnection.prepareCall
("BEGIN ship_door_assignment_pkg.assign_dock (?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?, ?,?,?,?); END;");
vCallStmt.setString(1, plantCode);
vCallStmt.setString(2, dock);
vCallStmt.setString(3, p1.palLbl);
vCallStmt.setFloat(4, p1.cartonQty);
vCallStmt.setFloat(5, p1.palSize);
vCallStmt.setFloat(6, p1.drumGallons);
vCallStmt.setFloat(7, p1.drumGross);
vCallStmt.setFloat(8, p1.drumTare);
vCallStmt.setString(9, p2.palLbl);
vCallStmt.setFloat(10, p2.cartonQty);
vCallStmt.setFloat(11, p2.palSize);
vCallStmt.setFloat(12, p2.drumGallons);
vCallStmt.setFloat(13, p2.drumGross);
vCallStmt.setFloat(14, p2.drumTare);
vCallStmt.setString(15, printer);
vCallStmt.registerOutParameter(16, OracleTypes.NUMBER);
vCallStmt.registerOutParameter(17, OracleTypes.VARCHAR);
vCallStmt.registerOutParameter(18, OracleTypes.CURSOR);
vCallStmt.registerOutParameter(19, OracleTypes.CURSOR);
vCallStmt.execute();
vRetCode = vCallStmt.getInt(16);
vRetMsg = vCallStmt.getString(17);
if (vRetCode == 0)
{
bolSummaryResultSet = (ResultSet)vCallStmt.getObject(18);
bolResultSet = (ResultSet)vCallStmt.getObject(19);
}
else
{ errorMsg = vRetMsg;
}
}
catch (SQLException se)
{
throw new RuntimeException(se);
}
finally
{
DBConnection.closeCallableStatement(vCallStmt);
DBConnection.closeConnection(vConnection);
}
return vRetCode;
} //assignDock
It's a lifecycle problem. If you close the database connection, all related result sets are also closed. (There is no connection to the database anymore. Don't ask me why it worked before.)
So you have to change your code such that it follows this sequence:
Run stored procedure and retrieve cursor/result set
Execute JSP page
Close database connection
I couldn't find any information if the result set retrieved as an out parameter of the stored procedure lives on after the statement has been closed. I think so. If not, you also need to wait before you close the statement.
The problem is that ResultSet must be closed too, and normally before the close of the statement. That statement or the connection might close the result set. However there are flags to make the result used longer, and even separate types. However common practice is to shovel a ResultSet to some List, and use that.
Try this:
vCallStmt = vConnection.prepareCall("BEGIN ship_door_assignment_pkg.assign_dock"
+ "(?,?, ?,?,?,?,?,?, ?,?,?,?,?,?, ?, ?,?,?,?); END;",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT);
The last option should work.

get table span class content using jsoup

I have a website that contains a table that look like similar(bigger..) to this one:
</table>
<tr>
<td>
<table width="100%" cellspacing="-1" cellpadding="0" border="0" dir="rtl" style="padding-top: 25px;">
<tr>
<td align="right" style="padding-right: 25px;">
<span class="artist_name_txt">
name
<p class="diccografia">subname</p>
</span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" dir="rtl" style="padding-right: 25px; padding-left: 25px">
<tr>
<td class="songs" align="right">
number1
</td>
</tr>
<tr>
<td class="songs" align="right">
number2
.......
</td>
</tr>
</table>
and I need an idea how can i parse the website and extract this table into 2 arrays -
one will be something like names{number1, number2}
and the second will be links{number1link, number2link}
I tried a lot of ways and nothing really helps me.
You should read the JSoup Cookbook - especially the Selector syntax is very powerful.
Here's an example:
final String html = ...
// use connect().get() instead if you connect to an website
Document doc = Jsoup.parse(html);
List<String> names = new ArrayList<>();
List<String> links = new ArrayList<>();
for( Element element : doc.select("a.artist_player_songlist") )
{
names.add(element.text());
links.add(element.attr("href"));
}
System.out.println("Names: " + names);
System.out.println("Links: " + links);
Output:
Names: [number1, number2]
Links: [/number1link, /number2link]
Android Web Scraping with a Headless Browser
Htmlunit on Android application
HttpUnit/HtmlUnit equivalent for android

Parsing table html with Jsoup

I'm try to parsing this table:
<table border="1" align="center" cellpadding="5" width="95%">
<tbody>
<tr>
<td colspan="2" align="center"> <b> <i> Test </i> </b> </td>
<td> <b> <i> Result </i> </b> </td>
<td> <b> <i> Credit </i> </b> </td>
<td> <b> <i> Data </i> </b> </td>
<td> <b> <i> A/A </i> </b> </td>
<td> <b> <i> Other data </i> </b> </td>
<td> <b> <i> A/A rif. </i> </b> </td>
</tr>
<tr>
<td> A000211 </td>
<td nowrap=""> Physic </td>
<td align="center"> - </td>
<td align="center"> 6 </td>
<td align="center"> - </td>
<td align="center"> 2008/2009 </td>
<td> something </td>
<td align="center"> 2007/2008 </td>
</tr>
<tr>
<td> 0065057 </td>
<td nowrap=""> Math </td>
<td align="center"> - </td>
<td align="center"> 6 </td>
<td align="center"> - </td>
<td align="center"> 2008/2009 </td>
<td> samething </td>
<td align="center"> 2008/2009 </td>
</tr>
<tr>
In java i have this, for now:
Document doc = Jsoup.parse(url);
Elements tables = doc.getElementsByTag("table");
I try to put this data in JsonObject i must iterate this tables? or there is a simple way?
i solved with:
Document doc = Jsoup.parse(url);
JSONObject jsonObject = new JSONObject();
JSONArray list = new JSONArray();
Element rows = doc.getElementsByTag("table tr");
for(Element row : rows) {
String Test = row.getElementsByTag("td").get(1).text();
String Result = row.getElementsByTag("td").get(2).text();
String Credit = row.getElementsByTag("td").get(3).text();
jsonObject.put("Test", Test);
jsonObject.put("Result", Result);
jsonObject.put("Credit", Credit);
}

Showing products in grid

I have a JSP page where I am showing all the products that fall under a specific category. The problem is that all of them either end up showing either vertically or horizontally based on whether I loop my "td" or "tr". I want to show them in grid where 3 products are in row 1, another 3 in row 2 and so on. Any idea on how this can be achieved?
ProductController.java
List<Product> productsLst = MasterDao.getAllProductsByCategory(new Integer(categoryId));
products.jsp
<table>
<tr>
<%
for (Product p : productsLst) {
%>
<td align="center">
<img src="../images/<%= p.getImage()%>" class="product-grid-img"/>
<br/><div id="product-name"><%= p.getName()%></div>
<br/><div id="money">$ <%= p.getListPrice()%></div>
</td>
<%
}
%>
</tr>
</table>
Just use a counter, if you got 3 products close the tr and open a new tr
Using Guava's Lists.partition() method:
List<List<Product>> rows = Lists.partition(productList);
page.setAttribute("rows", rows);
...
<c:forEach var="row" items="rows">
<tr>
<c:forEach var="product" items="row">
<td> ... details of the product ... </td>
</c:forEach>
</tr>
</c:forEach>
Already answer in this thread
<table>
<s:iterator value="productList" status="status">
<s:if test="#status.index %4 == 0">
<tr>
</s:if>
<td>
<img src="../product/image?imageID=<s:property value="productID"/>&type=thumbnail" />
</td>
<s:if test="#status.index %4 == 0">
</tr>
</s:if>
</s:iterator>
<table>

Categories