• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with this code  RSS feed

 
Lee Gary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi people, i am curently working on a code which will idsplay a pdf format to the user, i am using itext's packages to generate pdf files, but i have a problem here, i cant seem to display my pdf file when i insert another InitialContext. i am using IBM websphere studio, anyone one can help me on this? thanks a million~

my codes are as follows :

package com.ibm.ejs.container;

import hsbcPackage.Calculate;
import hsbcPackage.PersonalDetail;
import hsbcPackage.QuotationDetail;
import hsbcSession.CalculatePremium;
import hsbcSession.CalculatePremiumHome;
import hsbcSession.EnterPersonal;
import hsbcSession.EnterPersonalHome;
import hsbcSession.GenerateQuotation;
import hsbcSession.GenerateQuotationHome;

import java.awt.Color;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.Servlet;
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 com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

/**
* @version 1.0
* @author
*/
public class GeneratePDF extends HttpServlet implements Servlet {

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Document doc = new Document();

HttpSession session = req.getSession();
session.removeAttribute("quotationError");

try {


InitialContext ic = new InitialContext();
Object obj2 = ic.lookup("ejb/hsbcSession/CalculatePremiumHome");


CalculatePremiumHome cph =
(CalculatePremiumHome)PortableRemoteObject.narrow(obj2, CalculatePremiumHome.class);

CalculatePremium cp = cph.create();

Calculate cal = cp.RetrievePremium((session.getAttribute("quoID")).toString());

//Object obj = ic.lookup("ejb/hsbcsession/EnterPersonalHome");
//EnterPersonalHome eph = (EnterPersonalHome)PortableRemoteObject.narrow(obj , EnterPersonalHome.class);

//EnterPersonal ep = eph.create();

//PersonalDetail pd = ep.RetrievePersonal((session.getAttribute("nric")).toString());


PdfWriter.getInstance(doc, resp.getOutputStream());
doc.open();
// step 4

Paragraph p2 = new Paragraph(new Chunk("Your Quotation is as below : ",
FontFactory.getFont(FontFactory.COURIER, 10)));//"" +cal.getInsureCoe());
//Paragraph p1 = new Paragraph("Basic Premium :"+cal.getBasicPremium());
//Paragraph p2 = new Paragraph("Total Premium :"+cal.getTotal());
//Paragraph p3 = new Paragraph("Your quotationID is "+cal.getQuotationID());
//Paragraph p4 = new Paragraph("Date of quotation "+cal.getQuotationDate().toString());
doc.add(p2);
Table table = (Table) new Table(2);
Cell cell = (Cell) new Cell(new Paragraph("Personal Details"));
cell.setBackgroundColor(new Color(255, 0xC0, 0xC0));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(2);
cell.setLeading(10f);
cell.setLeft(20f);
table.addCell((Cell) cell);
table.addCell("Quotation ID");
table.addCell(cal.getQuotationID());
table.addCell("Basic Premium");
table.addCell(cal.getBasicPremium());
table.addCell("Premium Driver Discount");
table.addCell(cal.getPremiumDriver());
table.addCell("No Claim Discount");
table.addCell(cal.getNoClaimDis());
table.addCell("Net Premium Excess");
table.addCell(cal.getNetPremiumExcess());
table.addCell("Net Premium");
table.addCell(cal.getTotal());

doc.add(table);
//doc.add(p1);
//doc.add(p2);
//doc.add(p3);
//doc.add(p4);



doc.close();

} catch (DocumentException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
}

try
{
InitialContext ic2 = new InitialContext();
Object obj = ic2.lookup("ejb/hsbcsession/EnterPersonalHome");
EnterPersonalHome eph = (EnterPersonalHome)PortableRemoteObject.narrow(obj , EnterPersonalHome.class);

EnterPersonal ep = eph.create();

PersonalDetail pd = ep.RetrievePersonal((session.getAttribute("nric")).toString());


PdfWriter.getInstance(doc, resp.getOutputStream());
doc.open();


Paragraph p = new Paragraph(new Chunk("testing"));

doc.add(p);

doc.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
}
}




/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest,]
* javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

}
}
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the effect of closing the Document?

As an aside, it is good practice to close your Context when no longer needed.
 
Lee Gary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upon closing the document, a pdf file will be generated....... can i have 2 initialContext in one try{} loop?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upon closing the document, a pdf file will be generated

My suspicion is that you are having a problem by not creating a second Document object.

can i have 2 initialContext in one try{} loop?

Don't see why not, but it's not necessary. Just obtain your InitialContext once. You can declare a reference variable before the try block.
 
Lee Gary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have modified my codes, now, i can display the first pdf table, but not the second one....... i am lost

package com.ibm.ejs.container;

import hsbcPackage.Calculate;
import hsbcPackage.PersonalDetail;
import hsbcPackage.QuotationDetail;
import hsbcSession.CalculatePremium;
import hsbcSession.CalculatePremiumHome;
import hsbcSession.EnterPersonal;
import hsbcSession.EnterPersonalHome;
import hsbcSession.GenerateQuotation;
import hsbcSession.GenerateQuotationHome;

import java.awt.Color;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.Servlet;
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 com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

/**
* @version 1.0
* @author
*/
public class GeneratePDF extends HttpServlet implements Servlet {

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Document doc = new Document();
Document doc2 = new Document();
HttpSession session = req.getSession();
session.removeAttribute("quotationError");
InitialContext ic;
try {


ic = new InitialContext();
Object obj2 = ic.lookup("ejb/hsbcSession/CalculatePremiumHome");


CalculatePremiumHome cph =
(CalculatePremiumHome)PortableRemoteObject.narrow(obj2, CalculatePremiumHome.class);

CalculatePremium cp = cph.create();

Calculate cal = cp.RetrievePremium((session.getAttribute("quoID")).toString());




PdfWriter.getInstance(doc, resp.getOutputStream());
doc.open();
// step 4

Paragraph p2 = new Paragraph(new Chunk("Your Quotation is as below : ",
FontFactory.getFont(FontFactory.COURIER, 10)));//"" +cal.getInsureCoe());
//Paragraph p1 = new Paragraph("Basic Premium :"+cal.getBasicPremium());
//Paragraph p2 = new Paragraph("Total Premium :"+cal.getTotal());
//Paragraph p3 = new Paragraph("Your quotationID is "+cal.getQuotationID());
//Paragraph p4 = new Paragraph("Date of quotation "+cal.getQuotationDate().toString());
doc.add(p2);
Table table = (Table) new Table(2);
Cell cell = (Cell) new Cell(new Paragraph("Personal Details"));
cell.setBackgroundColor(new Color(255, 0xC0, 0xC0));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(2);
cell.setLeading(10f);
cell.setLeft(20f);
table.addCell((Cell) cell);
table.addCell("Quotation ID");
table.addCell(cal.getQuotationID());
table.addCell("Basic Premium");
table.addCell(cal.getBasicPremium());
table.addCell("Premium Driver Discount");
table.addCell(cal.getPremiumDriver());
table.addCell("No Claim Discount");
table.addCell(cal.getNoClaimDis());
table.addCell("Net Premium Excess");
table.addCell(cal.getNetPremiumExcess());
table.addCell("Net Premium");
table.addCell(cal.getTotal());

doc.add(table);
doc.close();

} catch (DocumentException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
}
try
{
ic = new InitialContext();
Object obj = ic.lookup("ejb/hsbcSession/EnterPersonalHome");
EnterPersonalHome eph = (EnterPersonalHome)PortableRemoteObject.narrow(obj , EnterPersonalHome.class);
EnterPersonal ep = eph.create();
PersonalDetail pd = ep.RetrievePersonal((session.getAttribute("nric")).toString());
PdfWriter.getInstance(doc2, resp.getOutputStream());
doc2.open();
Paragraph p = new Paragraph(new Chunk("testing" +pd.getEmployerName()));
doc2.add(p);


//doc.add(p1);
//doc.add(p2);
//doc.add(p3);
//doc.add(p4);
doc2.close();

} catch (DocumentException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
}
}




/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest,]
* javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!