sanitize a foreign entity when called in Java - 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>

Related

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.

Handling multiple tables using selenium webdriver

I am checking the folder hierarchy on a webpage, depending on the type of user. User1 has a set of permissions which enable him to see the folder structure like this :
Main Folder
- First Child
-First Grandchild
-Second Grandchild
- Second Child
- Third Child
Each branch of the tree is a table consisting of 1 row. But the number of columns varies depending on the generation.
The "Main Folder" parent has only 1 column. The cell content is the string "Main Folder".
The children branches have 2 columns, the first cell containing blank space, and the next cell containing the name of the branch ("First Child", "Second Child").
The grandchildren branches have 3 columns, the first and second cell containing blank space, and the the third cell containing the name of the branch (" First Grandchild", "Second Grandchild").
HTML code :
<div id = 0>
<div id = 1>
<table id = 1>
<tbody>
<tr>
<td id="content1"
<a id="label1"
<span id="treeNode1"
Main Folder
</span>
</a>
</td>
</tr>
</tbody>
</table>
<div id = 2>
<table id = 2>
<tbody>
<tr>
<td>
<td id="content2"
<a id="label2"
<span id="treeNode2"
First Child
</span>
</a>
</td>
</td>
</tr>
</tbody>
</table>
<div id = 5>
<table id = 5>
<tbody>
<tr>
<td>
<td>
<td id="content5"
<a id="label5"
<span id="treeNode5"
First GrandChild
</span>
</a>
</td>
</td>
</td>
</tr>
</tbody>
</table>
</div>
<div id = 6>
<table id = 6>
<tbody>
<tr>
<td>
<td>
<td id="content6"
<a id="label6"
<span id="treeNode6"
Second GrandChild
</span>
</a>
</td>
</td>
</td>
</tr>
</tbody>
</table>
</div>
</div> /* End of division 2 */
<div id = 3>
<table id = 3>
<tbody>
<tr>
<td>
<td id="content3"
<a id="label3"
<span id="treeNode3"
Second Child
</span>
</a>
</td>
</td>
</tr>
</tbody>
</table>
</div>
<div id = 4>
<table id = 4>
<tbody>
<tr>
<td>
<td id="content4"
<a id="label4"
<span id="treeNode4"
Third Child
</span>
</a>
</td>
</td>
</tr>
</tbody>
</table>
</div>
</div> /*End of division 1 */
</div> /* End of division 0 */
User2 has a different set of permissions, which enable him to see the folder structure like this :
Main Folder
- First Child
-First Grandchild
- Second Child
- Third Child
The corresponding table is absent in the html code for this user.
My test case is to check User2 doesn't have access to the second grandchild. This means I need to ensure that particular table doesn't exist on the webpage.
How can I check this in selenium ? I am using JUnit for my test cases. I want to do an "assert" to ensure the second grandchild is not present.
You'll want to check to see if the element is not present or not visible. Calling isElementVisible() inside an assert false should do the trick. Just get the By locator of the elements you want to check.
private boolean isElementVisible(By by)
{
try
{
return driver.findElement(by).isDisplayed();
}
catch(NoSuchElementException e)
{
return false;
}
}

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

using if else statement for dynamic table jsp

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>

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