I am not sure why <LINK REL="STYLESHEET" TYPE="text/css" HREF="C:/apache-tomcat-8.5.24/webapps/print.css"/> is not working, My css is not affecting nor supporting my html code and java code I am using to convert as pdf is this method, there is no error in PDF. Is there something I am missing?
#Override
public DataHandler Version(StringBuffer htmlSrc, String documentTitle) throws Exception {
DataHandler dataHandler = null;
String outputFile = "testNow.pdf";
try (ByteArrayOutputStream baos = htmlSrc;
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toString().getBytes());
BufferedInputStream inputStream = new BufferedInputStream(bais);
OutputStream os = new FileOutputStream(new File(outputFile));) {
ConverterProperties properties = new ConverterProperties();
HtmlConverter.convertToPdf(bais, os, properties);
dataHandler = new DataHandler(new FileDataSource(outputFile));
return dataHandler;
} catch (Exception e) {
log.error("Error # getPDFVersion():" + e);
}
return null;
}
.inetd1 { /* column row style use only with grid tables */
background-color : White;
/*border-style : solid;*/
font-weight : bold;
padding-bottom : 1px;
padding-left : 1px;
padding-right : 1px;
padding-top : 1px;
text-align : left;
/*border-color : Black;
border : thin;*/
font-size : xx-small;
}
<html>
<head>
<LINK REL="STYLESHEET" TYPE="text/css" HREF="C:/apache-tomcat-8.5.24/webapps/print.css"/>
<style> .pagebreak { page-break-after: always; }</style>
</head>
<div align="center">
<h4>ssssss</h4>
</div>
<table align="center" summary="Header d">
<tbody>
<tr valign="top">
<td align="right=" valign="top" class="inetd1">
<div align="right">Pr</div>
</td>
<td align="left=" class="inetd1"> Low Clinics</td>
</tr>
<tr valign="top">
<td align="right=" valign="top" class="inetd1">
<div align="right">Grae:</div>
</td>
<td align="left=" class="inetd1"> Lf Alabama</td>
</tr>
<tr valign="top">
<td align="right=" valign="top" class="inetd1">
<div align="right">Rep:</div>
</td>
<td align="left=" class="inetd1"> 424C</td>
</tr>
<tr valign="top">
<td align="right=" valign="top" class="inetd1">
<div align="right">Fut Period:</div>
</td>
<td align="left=" class="inetd1"> L8</td>
</tr>
<tr valign="top">
<td align="right=" valign="top" class="inetd1">
<div align="right">Report Period:</div>
</td>
<td align="left=" class="inetd1"> 01/01/2018 to 06/30/2018</td>
</tr>
<tr valign="top">
<td align="right=" valign="top" class="inetd1">
<div align="right">Repus:</div>
</td>
<td align="left=" class="inetd1"> Sad</td>
</tr>
</tbody>
</table>
<br/>
<div style="overflow-x:auto; width:90%; margin: 0 auto" id = "Group13950">
<table border = "1" width = "100%" align = "center" cellpadding = "2" cellspacing = "1" summary = "Form 13424-C | Low Income Taxpayer Clinic (LITC) - Advocacy Information header" class = "generaltable">
<tr>
<td valign="top" colspan = "1" rowspan = "1" class = "header">
<span id = Col183980><b>
<table border="1" bordercolor="#000000" style="border-style: none" cellspacing="1" cellpadding="1" width="100%" summary="Form n Date: XX/XX/XXXX" >
<td>
<table style="width:100%">
<tr>
<td align="left" style="position: float;">
<font style ="font-family: Arial, Helvetica, sans-serif; font-size: 10pt;">
Form
</font>
<font style ="font-family: Arial, Helvetica, sans-serif; font-size: 12pt;">
<B>134</B>
</font>
</td>
<td align="center" style="position: float;">
<font style ="font-family: Arial, Helvetica, sans-serif; font-size: 9 pt;">
Depasury - IntS
</font>
<br>
<font style ="font-family: Arial, Helvetica, sans-serif; font-size: 12pt;">
<B>Low
<br>Adrmation</B>
</td>
</td>
<td align="right" style="position: float;">
<font style ="font-family: Arial, Helvetica, sans-serif; font-size: 10pt;">
<B>OMB <br></B>15
</font>
</td>
</tr>
</td>
</table>
</tr>
</table>
</<html>
POM dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.2</version>
</dependency>
Related
Using itext and xmlworker version 5.5.13.2
I have a need to convert static HTML with basic CSS into a pdf. But the resulting PDF has not right format/ spacing/ position. Can someone help me out by pointing the mistake i have done or the something i have missed in the code part.
HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="margin:5px; width:100%">
<div style="border:1px solid black;padding: 25px;">
<div class="row1">
<div class="floatR" style="min-width: 33%;width: 33%;min-height: 1px; float: right;">
<h3>
Tax Invoice/Bill Of Supply/ Cash Memo
</h3>
(Original for Recipient)
</div>
</div>
<div class="row1 marginT50p">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<h4>
Sold By:
ArtisanIndia Shoppping Private Limited,
</h4>
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px ;float: left;">
<h4>
Sold By:
ArtisanIndia Shoppping Private Limited,
</h4>
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
</div>
<div class="row1 marginT25">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textUnderline">Comapny GSTIN:</label>JKDHFBGKJDJHJ
<br />
<label class="textUnderline">Comapny PAN:</label>SDJKHFGB
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textUnderline">Biller GSTIN:</label>JKDHFBGKJDJHJ
<br />
<label class="textUnderline">Biller PAN:</label>SDJKHFGB
</div>
</div>
<div class="row1 marginT25">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Order ID:</label>235235
<br />
<label class="textBold">Order Date:</label>12/12/1912
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Shipping Address:</label>
<br />
<label class="textBold"> </label>
<br />
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
</div>
<div class="row1 marginT25">
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Invoice Date:</label>12/12/1912
<br />
<label class="textBold">Order ID:</label>AI/2012/907656767
</div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;"></div>
<div style="min-width: 33%;width: 33%;min-height: 1px;float: left;">
<label class="textBold">Place Of Supply:</label> Maharastra
<br />
<label class="textBold">Place Of Delivery:</label>Maharastra
</div>
</div>
<div class="row1 marginT25">
<table style="width:100%">
<tr>
<th>Sr. No.</th>
<th>Product</th>
<th>Description</th>
<th>HSN</th>
<th>GST %</th>
<th>QTY</th>
<th>Gross Amount</th>
<th>Discount</th>
<th>Taxable value</th>
<th>CGST</th>
<th>SGST</th>
<th>IGST</th>
<th>Total</th>
</tr>
<tr>
<td>1</td>
<td>Product Description</td>
<td>Item Description</td>
<td>123456</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>Product Description</td>
<td>Item Description</td>
<td>123456</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>Product Description</td>
<td>Item Description</td>
<td>123456</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="5">Total</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
<div class="row1">
<div style="min-width: 25%; width: 25%; min-height: 1px;float: left;">
<label class="textUnderline textBold">
Seller Registered Address:
ArtisanIndia Shoppping Private Limited,
</label>
Shop On Plot No. 7 and 8, 41 and 42, Vijay Nagar,
Patrakar Colony Road, Mansarovar,
Jaipur - 302020, Rajasthan
State/UT Code: 08
Country: India
</div>
</div>
<div class="row1 marginT50p">
<div style="min-width: 25%; width: 25%; min-height: 1px;float: left;">
E. & O. E.
</div>
<div class=" floatR" style="min-width: 25%; width: 25%; min-height: 1px; float: right;">
<div class="signbox">
Sign/Stamp
</div>
<span> Ordered Through
</span>
<span class="textUnderline">
ArtisanIndia Shoppping Pvt Limited
<br />
Authorized Signature
</span>
</div>
</div>
</div>
</div>
</body>
</html>
CSS
* {
font-family: sans-serif;
font-size:8px
}
.width33 {
min-width: 33%;
width: 33%;
min-height: 1px;
border:1px solid red
}
.width25 {
min-width: 25%;
width: 25%;
min-height: 1px;
border: 1px solid green;
}
.row1:after {
content: "";
display: table;
clear: both;
}
.floatL {
float: left;
}
.floatR {
float: right;
}
.marginT25 {
margin-top: 25px;
}
.marginT50p {
margin-top: 50px
}
.textUnderline {
text-decoration: underline;
}
.textBold {
font-weight: bold;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
font-size: 10px;
}
th,
td {
height: 40px;
text-align: center;
}
.signbox {
vertical-align: middle;
text-align: center;
height: 50px;
border: 1px solid black;
/* margin: auto; */
}
Java
UploadUtil s3Upload = new UploadUtil();
PdfWriter pdfWriter = null;
ByteArrayOutputStream gg = new ByteArrayOutputStream();
try{
Document document = new Document();
PdfWriter.getInstance(document, gg);
document.open();
XMLWorkerHelper xmlWorkerHelper = XMLWorkerHelper.getInstance();
xmlWorkerHelper.parseXHtml(pdfWriter, document, new FileInputStream("C:\\Users\\jj\\Desktop\\asdf\\tt.html"), new FileInputStream("C:\\Users\\jj\\Desktop\\asdf\\ttt.css"));
document.close();
FileOutputStream fos = new FileOutputStream("C:\\Users\\jj\\Desktop\\asdf\\test.pdf");
fos.write(gg.toByteArray());
fos.close();
}
catch(Exception e){
System.out.println("asdf");
}
the resulting pdf generated looks something like this.
instead of
Can someone out here point out the reason why this is happening or what I have missed or done wrong?
iText 5.5.13.2 is the old version of iText product and it's not supported anymore. Most likely there are some not supported functionality in the html or css. I can guess that float properties are not supported. Probably there are issues with percentage width. You can try to play around with your html and css files to try to make pdf look acceptable, but the easiest way is to use the newest version of iText product which is iText 7
How we can get the Table(in a webpage) row using the value it contain.
Suppose a table of 3*3. In that cell(2,3) cell text is "Test". So how to find the row number = 2 using the the cell text "Test".
Selenium-2.53.1
Java-8
This is the xpath
//table[2]/tbody/tr/td[2]/table[2]/tbody/tr[7]/td/div[2]/table/tbody/tr[1]/td[1]/div/a/font
in which bolded tr (here tr[1]) is the row I want to change dynamically & the text is inside the bolded td (here td[1])
My html code is :
<html>
<head></head>
<body>
<form name="fm" method="post">
<table>
<!-- --Some data--- -->
</table>
<table width="100%" height="100%" border="0" >
<tr valign="top" >
<td width="200" height="100%" rowspan="3" align="center" >
<p></p>
<table width="89%" height="270" border="0">
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<!-- --Some data--- -->
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td><table width="100%" height="71" border="0" align="right">
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table></td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<!-- --Some data--- -->
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
<p></p>
</td>
<td width="622" height="30" colspan="3" align="left" valign="top">
<table width="100%" height="40" align="center">
<tr><!-- --Some data--- --></tr>
</table>
<table width="476" height="110" align="center">
<tr></tr>
<tr></tr>
<tr></tr>
<!-- --Some data--- -->
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td height="22" colspan="3">
<div align="left">
<table width="469" border="1">
<tr bgcolor="#006291">
<td width="81" bgcolor="#006291"><div align="center">SlNo.</div></td>
<td width="82" bgcolor="#006291"><div align="center">Code</div></td>
<td width="300" bgcolor="#006291"><div align="center">Name</div></td>
</tr>
</table>
</div>
<div align="left" style="width:465px;Height:150px;">
<table width="465" bgcolor="#FFFFFF" border="1">
<tr bordercolor="#006291" bgcolor='#FFEADF' >
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >1</font></div></td>
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >01</font></div></td>
<td width="300" height='5' valign="top"><div align="center"><font color="#006291" size="3" >tvm1</font></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#F1F8FC' >
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >2</font></div></td>
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >02</font></div></td>
<td width="300" height='5' valign="top"><div align="center"><font color="#006291" size="3" >tvm2</font></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#FFEADF' >
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >3</font></div></td>
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >03</font></div></td>
<td width="300" height='5' valign="top"><div align="center"><font color="#006291" size="3" >tvm3</font></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#F1F8FC' >
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >4</font></div></td>
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >04</font></div></td>
<td width="300" height='5' valign="top"><div align="center"><font color="#006291" size="3" >tvm4</font></div></td>
</tr>
<tr bordercolor="#006291" bgcolor='#FFEADF' >
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >5</font></div></td>
<td width="81" height='5' valign="top"><div align="center"><font color="#006291" size="3" >10</font></div></td>
<td width="300" height='5' valign="top"><div align="center"><font color="#006291" size="3" >tvm5</font></div></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
"count(//table[#id='tableid']//td[.='Test']/parent::tr/preceding-sibling::tr) + 1"
put in your own table identifier... depends on it being a simple table without rowspans and colspans.
I tried capturing the column data of all rows into a List(observe the xpath of 'rowList' below, no number given for the final 'tr' which gets the column data for all rows), and using foreach got the index of the row which contains required text.
Will have to change the value in 'expRowValue' to have it worked with different data.
public class Table {
WebDriver driver;
By rowList = By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[3]/td/table/tbody/tr/td[2]");
#Test
public void f() {
String expRowValue="$513";
System.setProperty("webdriver.chrome.driver", "C:/Users/dell/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
driver.manage().window().maximize();
List<WebElement>rowsData = driver.findElements(rowList);
int k=1;
for (WebElement rowValue: rowsData) {
if (rowValue.getText().equals(expRowValue)){
System.out.println("ROw Index::" + k);
}
k++;
}
}
}
If you want to click on <td> on the basis of text try below xpath :-
.//td[child::a/font[text() = 'your text hare']]
Or if you to click on <a> inside <td> on the basis of text try below xpath :-
.//td/a[child::font[text() = 'your text hare']]
Or it you want to click on table cell using their row and column index follow this link
URL oracle = new URL("hurlAddress/dinfo.cgi");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
I get
<html>
<head>
<!-- RUI RC=100 -->
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Don't open this page!</title>
</head>
<body bgcolor="#C0C0C0">
<center>
<table border="0" cellpadding="0" cellspacing="4" width="400">
<tr><td align="center" valign="top" width="48">
<img src="en/media/ss_wrn.gif" border="0" width=32 height=32>
</td>
<td>
<font size="4" face="Helvetica,Arial">
<b>User information is disabled.</b><br>
This operation cannot be accepted. User certification is invalid or date expired.<br>
Update page.<br>
</font>
</td>
</tr>
<tr><td></td>
<td align="right">
<img src="en/media/b_ok.gif" border="0" alt="OK" title="OK">
</td>
</tr>
</table>
</center>
</body>
I was expecting
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Remote UI:Device Information</title>
</head>
<body bgcolor="white" link="blue" alink="red" vlink="blue">
<a name="page_top"></a>
<form>
<SCRIPT LANGUAGE="JavaScript">
var has_fax = false; // true or false
var Fax_Type = "0"; // 0(None),1(PSTN),2(PSTNx2),3(ISDN)
var has_send =true; // has send
var is_oem = false;
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function reload_device(){
var cgi_str = "./dinfo.cgi?";
// for Expire Cache!!
var now_time = new Date();
cgi_str += "Dummy=" + now_time.getTime();
document.location.href = cgi_str;
}
</SCRIPT>
<table border="0" cellpadding="0" cellspacing="2" width="100%">
<tr>
<td valign="middle"><font size="4" color="#000066"><b><table border="0" cellpadding="0" cellspacing="2">
<tr>
<td rowspan="2" nowrap><img src="en/media/imgs030.gif"></td>
<td nowrap><font face="Helvetica,Arial" size="4" color="#000066"><b>slq29986</b></font></td>
</tr>
<tr>
<td nowrap><font size="2">iR5570</font></td>
</tr>
</table></b></font></td>
<td valign="middle" width="70" align="right"><font size="4" color="black"><b><img src="en/media/bh_updt.gif" width="28" height="28" border="0" alt="Update" title="Update"></b></font></td>
</tr>
</table>
<hr noshade size="2">
<table border="0" cellpadding="0" cellspacing="4" width="100%">
<tr>
<td align="left"><font size="1"> Last Updated :05/26/2016 19:30:54</font></td>
</tr>
</table><table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr height="26">
<td bgcolor="#000066" height="26"><font color="white" face="Helvetica,Arial"><b> Device Information</b></font></td>
</tr>
</table><table border="0" cellspacing="2" cellpadding="2" width="100%">
<SCRIPT LANGUAGE="JavaScript">
if(is_oem == false)
{
document.write('<tr>');
document.write('<td width="3%"></td>');
document.write('<td width="40%"><font face="Helvetica,Arial" size="2">Manufacturer :</font></td>');
document.write('<td><font size="4">CANON INC</font></td>');
document.write('</tr>');
}
</SCRIPT>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Device Name :</font></td>
<td><font size="4">slq29986</font></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Location :</font></td>
<td><font size="4">MESAAS</font></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Product Name : </font></td>
<td>iR5570</td>
</tr>
<tr>
<td colspan="3"><hr size="1"></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Serial Number :</font></td>
<td>SLQ29986</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Main Board Version :</font></td>
<td>1</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Controller Version :</font></td>
<td>3604.838.3201</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Scanner Version :</font></td>
<td>401.100</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Finisher Version :</font></td>
<td>801</td>
</tr>
<SCRIPT LANGUAGE="JavaScript">
if(has_send == true)
{
document.write('<tr>');
document.write('<td colspan="3">');
document.write('<hr size="1">');
document.write('</td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td width="3%"></td>');
document.write('<td width="40%"><font face="Helvetica,Arial" size="2">Receive E-mail Address :</font></td>');
document.write('<td>aaa#aaa.edu</td>');
document.write('</tr>');
}
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
if( has_fax == true )
{
document.write('<tr>');
document.write('<td colspan="3">');
document.write('<hr size="1">');
document.write('</td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td width="3%"></td>');
document.write('<td width="40%"><font face="Helvetica,Arial" size="2">Telephone Number :</font></td>');
document.write('<td></td>');
document.write('</tr>');
if( Fax_Type == "3" )
{
document.write('<tr>');
document.write('<td width="3%"></td>');
document.write('<td width="40%"><font face="Helvetica,Arial" size="2">Subaddress :</font></td>');
document.write('<td></td>');
document.write('</tr>');
}
}
</SCRIPT>
<tr>
<td colspan="3">
<hr size="1">
</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">System Manager :</font></td>
<td>MESAAS</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Contact Information :</font></td>
<td></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">E-mail Address :</font></td>
<td></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Support :</font></td>
<td></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Administrator Comment (E-mail) :</font></td>
<td></td>
</tr>
<tr>
<td colspan="3">
<hr size="1">
</td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Contact Person :</font></td>
<td></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Phone :</font></td>
<td></td>
</tr>
<tr>
<td width="3%"></td>
<td width="40%"><font face="Helvetica,Arial" size="2">Comment (E-mail) :</font></td>
<td></td>
</tr>
</table>
<p>
<hr size="2">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td valign="bottom"><img src="en/media/ba_ptp.gif" border="0" hspace="2" vspace="2" alt="Back to the Top on This Page" title="Back to the Top on This Page"></td>
</tr>
</table>
</form>
</body>
</html>
Thanks to amitmah and Gilbert Le Blanc I installed
httpfox addon on firefox and I noticed that I need to set a cookie
URL url = new URL("urlAddress/dinfo.cgi");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie", "iR=7472571");
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))){
StringBuilder builder = new StringBuilder();
int cp;
while ((cp = in.read()) != -1) {
builder.append((char) cp);
}
return builder.toString();
}
In browser this cookie is set if I access first urlAddress. If I tried to access directly urlAddress/dinfo.cgi I get the same "User information is disabled..." response.
I'm trying to link a product from the category.jsp page to its own product.jsp page. When I click on a given product on the category page, the link in the browser displays perfectly as supposed to: http://localhost:8080/ecommerce/product?389 but the issue: I receive a HTTP Status 404 - Not Found error. I know it's because it's not finding the page, but after trying several solutions... I seem to not be able to find the piece of code that would make a difference... Any help would be much appreciated
The Controller Servlet:
package controller;
import cart.ShoppingCart;
import wishlist.Wishlist;
import entity.Category;
import entity.Product;
import java.io.IOException;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.CategoryFacade;
import session.ProductFacade;
public class ControllerServlet extends HttpServlet {
#EJB
private CategoryFacade categoryFacade;
#EJB
private ProductFacade productFacade;
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userPath = request.getServletPath();
HttpSession session = request.getSession();
Category selectedCategory;
Product selectedProduct;
Collection<Product> categoryProducts;
// if product page is requested
if (userPath.equals("/product")) {
// get productId from request
String productId = request.getQueryString();
if (productId != null) {
// get selected product
selectedProduct = productFacade.find(Short.parseShort(productId));
// place selected product in session scope
session.setAttribute("selectedProduct", selectedProduct);
}
This is the code from the category.jsp to call the product:
<img class="img" src="${initParam.productImagePath}${product.name}.jpg">
Full Category.jsp:
<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">
<c:forEach var="product" items="${categoryProducts}" varStatus="iter">
<td>
<tbody>
<tr>
<td
style="vertical-align: middle; width: 180px; text-align: center; height: 180px;"
class="cell"><img class="img" src="${initParam.productImagePath}${product.name}.jpg"><br>
</td>
<td
style="vertical-align: middle; width: 140px; text-align: center;">${product.name}<br>
</td>
<td
style="vertical-align: middle; width: 125px; text-align: center;">$
${product.price}<br>
</td>
<td
style="vertical-align: middle; width: 125px; text-align: center;">
<form action="addToWishlist" method="post"><br><br> <input
name="productId" value="${product.id}" type="hidden">
<input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit"> </form>
<br>
</td>
<td
style="vertical-align: middle; width: 123px; text-align: center;">
<form action="addToCart" method="post"><br><br>
<input name="productId"
value="${product.id}" type="hidden"> <input class="submit" value="<fmt:message key='AddToCart'/>"
type="submit">
</form>
<br>
</td>
</tr>
</tbody>
</c:forEach>
</table>
This is the Product.jsp (destination page):
<table style="text-align: left; width: 100%; height: 172px;" border="0"
cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td colspan="1" rowspan="6" style="vertical-align: top;">product_gallery<br>
</td>
<td colspan="1" rowspan="6" style="vertical-align: top;"><img class="img" src="${initParam.productBigImagePath}${product.name}.jpg"><br>
</td>
<td style="vertical-align: top;">${selectedProduct.name}<br>
</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td style="vertical-align: top;">$ ${selectedProduct.price}</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td style="vertical-align: top;"><br>
</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td style="vertical-align: top;"><br>
</td>
<td style="vertical-align: top;"><br>
</td>
</tr>
<tr>
<td colspan="2" rowspan="1" style="vertical-align: top;">${selectedProduct.description}</td>
</tr>
<tr>
<td style="vertical-align: top;"><form action="addToWishlist" method="post"><br><br> <input
name="productId" value="${product.id}" type="hidden">
<input class="submit" value="<fmt:message key='AddToWishlist'/>" type="submit"> </form><br>
</td>
<td style="vertical-align: top;"><form action="addToCart" method="post"><br><br>
<input name="productId"
value="${product.id}" type="hidden"> <input class="submit" value="<fmt:message key='AddToCart'/>"
type="submit">
</form></td>
</tr>
<tr>
<td style="vertical-align: top;"><br>
</td>
<td style="vertical-align: top;"><br>
</td>
<td colspan="2" rowspan="1" style="vertical-align: top;"><ul><li style="background-color: rgb(198, 255, 201); width:100%; text-align:center; border-radius:2em;"><fmt:message key='ContinueShopping'/></li>
</ul><br>
</td>
</tr>
</tbody>
</table>
Solved the issue myself. I forgot to add the parameter in the web.xml:
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/product</url-pattern>
</servlet-mapping>
i have a jsp page with jdbc connection and on first load it shows the data accurately but after that it shows empty tables i think 2nd time it loads from memory not from server
what is problem behind i don't know
ok here are the details
i have a servlet that maintains the session for a user that log in and then after creating the session the servlet redirect the user to a view page that is a jsp page and displays the existing records in DB
when the servlet redirects the page the jsp can show the records in but when i access this page from any other html page it is unable to display the records here is the code for jsp page to view.
<%# page import="java.sql.ResultSet" %>
<%# page import="java.math.BigDecimal" %>
<%# page import="java.sql.SQLException" %>
<%# page import="java.util.logging.Level" %>
<%# page import="java.util.logging.Logger" %>
<%# page import="iEHR.cDBProcessor" %>
<HTML>
<HEAD>
<TITLE>View Patient</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1251">
<script type="text/javascript" language="javascript" src="datepicker/main.js"></script>
<script type="text/javascript" language="javascript" src="datepicker/prototype-1.js"></script>
<script type="text/javascript" language="javascript" src="datepicker/prototype-base-extensions.js"></script>
<script type="text/javascript" language="javascript" src="datepicker/prototype-date-extensions.js"></script>
<script type="text/javascript" language="javascript" src="datepicker/behaviour.js"></script>
<script type="text/javascript" language="javascript" src="datepicker/ratingbar.js"></script>
<script type="text/javascript" language="javascript" src="datepicker/datepicker.js"></script>
<link rel="stylesheet" href="datepicker/datepicker.css">
<script type="text/javascript" language="javascript" src="datepicker/behaviors.js"></script>
<style type="text/css">
<!--
img {
border: none;
}
.tah10 {
font-family: "Times New Roman", Times, serif;
font-size: 14px;
text-decoration: none;
color: #000000;
font-style: italic;
}
.tah11 {
font-family: Tahoma;
font-size: 11px;
text-decoration: none;
color: #000000;
}
.ver10 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
text-decoration: none;
color: #000000;
}
.ver11 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 11px;
text-decoration: none;
color: #000000;
}
.tah9 {
font-family: Tahoma;
font-size: 9px;
text-decoration: none;
color: #000000;
}
.ver9 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 9px;
text-decoration: none;
color: #000000;
}
td {
vertical-align: top;
color: 497B99;
font-size: 12px;
font-style: normal;
font-weight: bolder;
}
-->
</style>
<style type="text/css">
<!--
.bgtop {
background-repeat: repeat-x;
background-position: top;
}
-->
</style>
<style type="text/css">
<!--
a {
font-family: Times New Roman, Times, serif;
font-size: 12px;
font-weight: bolder;
color: 467B99;
text-decoration: none;
}
.datepicker{
font-family: Times New Roman, Times, serif;
font-size: 12px;
font-weight: bolder;
color: 467B99;
text-decoration: none;
}
a:hover {
font-size: 10px;
font-weight: bold;
color: FF8400;
}
.style1 {
color: 467B99;
font-family: Times New Roman, Times, serif;
font-size: 12px;
text-decoration: none;
font-style: normal;
font-weight: bolder;
}
.style2 {
font-family: Times New Roman, Times, serif;
font-size: 12px;
font-style: normal;
font-weight: bolder;
color: 467B99;
text-decoration: none;
}
-->
</style>
</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
<!-- ImageReady Slices (0005_red.psd - Slices: 03, 04, 05) -->
<TABLE WIDTH=100% height="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0 bgcolor="#FFFFFF">
<TR>
<TD width="12%">
<TABLE WIDTH=159 BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD COLSPAN=6>
<IMG SRC="images/logo.gif" WIDTH=159 HEIGHT=128 ALT=""></TD>
</TR>
<TR>
<TD>
<IMG SRC="images/s1.gif" WIDTH=20 HEIGHT=20 ALT=""></TD>
<TD>
<A HREF="#">
<IMG SRC="images/r1.gif" WIDTH=29 HEIGHT=20 BORDER=0 ALT=""></A></TD>
<TD>
<A HREF="#">
<IMG SRC="images/r2.gif" WIDTH=31 HEIGHT=20 BORDER=0 ALT=""></A></TD>
<TD>
<A HREF="#">
<IMG SRC="images/r3.gif" WIDTH=31 HEIGHT=20 BORDER=0 ALT=""></A></TD>
<TD>
<A HREF="#">
<IMG SRC="images/r4.gif" WIDTH=28 HEIGHT=20 BORDER=0 ALT=""></A></TD>
<TD>
<IMG SRC="images/s2.gif" WIDTH=20 HEIGHT=20 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=6>
<IMG SRC="images/s3.gif" WIDTH=159 HEIGHT=93 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=6>
<IMG SRC="images/h1.jpg" WIDTH=159 HEIGHT=32 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=6>
<IMG SRC="images/img1.jpg" WIDTH=159 HEIGHT=76 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=6>
<IMG SRC="images/img2.jpg" WIDTH=159 HEIGHT=81 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=6>
<IMG SRC="images/img3.jpg" WIDTH=159 HEIGHT=79 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=6> <IMG SRC="images/but1.jpg" ALT="" WIDTH=159 HEIGHT=42 border="0" usemap="#Map"></TD>
</TR>
</TABLE> </TD>
<TD width="12%">
<TABLE WIDTH=167 BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/h2.jpg" WIDTH=167 HEIGHT=25 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg1.gif" HEIGHT=94>
<div style="padding:20;padding-top:5;padding-right:10;padding-bottom:0;color:ffffff" class="tah10">
<strong>Noesis</strong><br><br>
<strong>Inovative EHR Services </strong> <br>
<br>
</div> </TD>
</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/s4.jpg" WIDTH=167 HEIGHT=52 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg2.gif">
<div style="padding-left:0px;padding-top:12px;padding-bottom:2"><span style="padding-left:20;padding-top:5">Add Patient </span></div> </TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg3.gif" HEIGHT=18>
<div style="padding-left:20;padding-top:5">Add Patient History </div>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg4.gif" HEIGHT=18>
<div style="padding-left:20;padding-top:5">Add Patient Insurance</div> </TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg5.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5">View Patient Records</div></TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg6.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5">About Us</div> </TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg7.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5">Contact Us</div> </TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg8.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5"></div> </TD>
</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/h3.gif" WIDTH=167 HEIGHT=65 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2 background="images/bg9.gif" HEIGHT=94>
<div style="padding:15;padding-top:3;padding-bottom:3;color:737373" class="tah10"></div> </TD>
</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/h4.jpg" WIDTH=167 HEIGHT=41 ALT=""></TD>
</TR>
<TR>
<TD background="images/bg10.jpg" WIDTH=123 HEIGHT=32>
<div style="padding-left:15;padding-top:1">
<input name="text" type="text" size="11">
</div></TD>
<TD width="44"> <IMG SRC="images/but2.jpg" ALT="" WIDTH=44 HEIGHT=32 border="0" usemap="#Map2"></TD>
</TR>
<TR>
<TD COLSPAN=2>
<IMG SRC="images/s5.jpg" WIDTH=167 HEIGHT=48 ALT=""></TD>
</TR>
<TR>
<TD COLSPAN=2> </TD>
</TR>
</TABLE> </TD>
<TD width="72%">
<table width=100% height="193" border=0 cellpadding=0 cellspacing=0>
<tr>
<td height=172 colspan="4" ><span class="bgtop"><img src="images/f_m.jpg"></span></td>
<td height=172 colspan="2" width="63%" background="images/bg_tile_1.gif" class="bgtop"> </td>
</tr>
<tr>
<td height=19 colspan="4" background="images/bg11.gif" > </td>
</tr>
</table>
<%! private ResultSet rsResult; %>
<%! cDBProcessor DBProcess = new cDBProcessor(); %>
<%
DBProcess.ConnectTODB();
if(request.getAttribute("dbrec") != null)
{
rsResult = DBProcess.statement.executeQuery("SELECT * FROM patients");
}//end if
out.println("<table border=\"0\"><tr><td valign=\"top\" >");
out.println("<tr><th>Patient ID</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Gender</th><th>Marital Status</th><th>Phone No.</th><th>Address</th>");
out.println("<th>Date Of Birth</th><th>Last Date Of Exam</th><th>Status</th></tr>");
//data display on page
if(rsResult!= null)
{
try
{
while (rsResult.next())
{
BigDecimal bdPatientID = rsResult.getBigDecimal("patient_id");
String strFirstname = rsResult.getString("first_name");
String strLastname = rsResult.getString("last_name");
String strMiddlename = rsResult.getString("middle_name");
String strGeneder = rsResult.getString("gender");
String strMeritalStatus = rsResult.getString("marital_status");
BigDecimal bdPhoneNo = rsResult.getBigDecimal("phone_no");
String strAddress = rsResult.getString("address");
String strDOB = rsResult.getDate("birth_dt") == null ? "" : rsResult.getDate("birth_dt").toString();
String strDOE = rsResult.getDate("dt_of_exam") == null ? "" :rsResult.getDate("dt_of_exam").toString();
String strStatus;
Byte bPatientStatus= rsResult.getByte("status");
if(bPatientStatus == 1)
{
strStatus = "Active";
}//end if
else
{
strStatus = "Inactive";
}//end else
out.println("<tr><td>"+bdPatientID+"</td><td>"+strFirstname+"</td><td>"+strMiddlename+"</td><td>"+strLastname+"</td>");
out.println("<td>"+strGeneder+"</td><td>"+strMeritalStatus+"</td><td>"+bdPhoneNo+"</td><td>"+strAddress+"</td>");
out.println("<td>"+strDOB+"</td><td>"+strDOE+"</td><td>"+strStatus+"</td></tr>");
}//end while
out.println("</table>");
}//end try
catch (SQLException ex)
{
out.println("<I>exception</I><br>");
}//end catch
}//end if
DBProcess.CloseDB();
%>
</TD>
<TD width="4%" background="images/bg_tile_1.gif" class="bgtop"> </TD>
</TR>
<TR>
<TD height="100%" colspan="4"> </TD>
</TR>
<TR>
<TD colspan="3">
<TABLE WIDTH=768 BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD width="326">
<IMG SRC="images/s7.gif" WIDTH=326 HEIGHT=48 ALT=""></TD>
<TD background="images/bg16.gif" WIDTH=442 HEIGHT=48>
<div style="padding-top:12;color:A8A8A8" class="tah11">
2010 © Copyright iAS. <Br>
All rights Reserved. Read Privacy Policy.</div> </TD>
</TR>
</TABLE> </TD>
<TD width="4%" background="images/bg_tile_2.gif"> </TD>
</TR>
</TABLE>
<!-- End ImageReady Slices -->
<map name="Map">
<area shape="rect" coords="39,2,133,27" href="#">
</map>
<map name="Map2">
<area shape="circle" coords="9,12,9" href="#">
</map>
</BODY>
</HTML>
It smells like threadsafety/scoping problem. You've declared the ResultSet and DBProcess as instance variable of the JSP using <%! %> scriptlet declarations, so it's been shared among all HTTP requests. I am not sure about the DBProcess, but this is certainly a bad idea for ResultSet (and also for Connection and Statement by the way). How the DBProcess is been used in the remnant of the code is also pretty scary, e.g. DBProcess.statement.executeQuery(). Is the Statement really a public field? I don't know how the class' internals look like, but yes, this smells too much like threadsafety/scoping problem.
Also, this 90's style writing of HTML and using scriptlets in JSP really don't suit a "Copyright 2010" application. Are you reading the right tutorials/books?
beside those issues pointed out by BalusC,
base on the described symptom "when the servlet redirects the page the jsp can show the records in but when i access this page from any other html page it is unable to display the records" and code "if(request.getAttribute("dbrec") != null) ".
My prime suspect for the issue's cause is the use of request.getAttribute("dbrec")
for those direct access from other html page, this value likely to be null. I suspect for redirect case, there may be some code that perform request.setAttribute("dbrec",....)