• Post Reply Bookmark Topic Watch Topic
  • New Topic

java client communicating with servlet  RSS feed

 
Denise L Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working with a java application and would like to kick off a web servlet. I've been successful accessing the servlet using a POST and passign in parameters. the only problem is that the servlet doesn't open it's itext document but rather sends it back to the client application. I don't fully understand the code that runs the post to the servlet. I know that If I don't use the line
InputStream rawInStream = conn.getInputStream();
that the servlet doesn't get accessed. Should I be using something else?
If i use the code that is commented out then the itext document is in the StringBuffer

Here is the code <--Special thanks to Martin Roth for the code -->

Client code:
public void flaggedReport(java.awt.event.ActionEvent actionEvent) {
String lUrl = "http://localhost:9080/Servlet/servletName";

String body="presentationtype=pdf&joe=dls";

/** Post a string to an URL and get the reply as a string. Returns an empty
string if things didn't work out. */

StringBuffer sb = new StringBuffer();

// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";
}

try {
URL url = new URL(lUrl);
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(false); // you may not ask the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it anyway
conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" );
// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length", Integer.toString(body.length()));

// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);

pw.print(body); // here we "send" our body!
pw.flush();
pw.close();

// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();



////// get response
//BufferedReader rdr = new BufferedReader(new InputStreamReader(rawInStream));
//String line;

//while ((line = rdr.readLine()) != null) {
//sb.append(line);
//sb.append(newline);
//}

//System.out.println("sb is " + sb.toString());
//return sb.toString();
} catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
// return ""; // an exception occurred

}

Servlet Code:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

//get the content to send to the pdf
DataClass data = new DataClass();
try {

data.execute();
//get the info
setColumnCnt(data.getDBSelect().getColumnCount());
System.out.println("columnCnt = " + getColumnCnt());
setRecordCnt(data.getDBSelect().getRowCount());

} catch (SQLException e) {
e.printStackTrace();
}

// we retrieve the presentationtype

String presentationtype = request.getParameter("presentationtype");
String joe = request.getParameter("joe");
System.out.println("We're here. Presentationtype is " + presentationtype + " joe is " + joe);

// step 1
/*
* dls-by default the page is portrait to create a landscape format just
* rotate the page
*/
Document document = new Document(PageSize.A4.rotate());
try {
// step 2: we set the ContentType and create an instance of the
// corresponding Writer
if ("pdf".equals(presentationtype)) {
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
} else if ("html".equals(presentationtype)) {
response.setContentType("text/html");
HtmlWriter.getInstance(document, response.getOutputStream());
} else if ("rtf".equals(presentationtype)) {
response.setContentType("text/rtf");
RtfWriter2.getInstance(document, response.getOutputStream());
} else {

response.sendRedirect("http://www.ongov.net");
}

// step 3
//PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("endpage.pdf"));
// writer.setPageEvent(new EndPage());

document.open();

//report Title
document.add(new Paragraph("Flagged Candidate Report\n\n", new Font(
Font.HELVETICA, 20, Font.BOLD )));
//dls-make a table
/** ****************************************************************** */

// add content
SimpleTable table1 = new SimpleTable();
//table1.setBorderColor(Color.WHITE);
table1.setWidthpercentage(100);
table1.setCellpadding(2);
table1.setCellspacing(2);

//Table Title
SimpleCell row = new SimpleCell(SimpleCell.ROW);
// row.setBackgroundColor(Color.yellow);
SimpleCell cell1 = new SimpleCell(SimpleCell.CELL);

//Table headings
//column 1
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.setWidth(4f);

cell1.add(new Paragraph(""));
row.add(cell1);
//end column 1
//column 2
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.setWidth(20f);
// cell1.setWidthpercentage(20f);
cell1.add(new Paragraph("Name", new Font(Font.HELVETICA, 12,
Font.BOLD)));

row.add(cell1);
//end column 2
//column 3
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.setWidth(10f);
// cell1.setWidthpercentage(10f);
cell1.add(new Paragraph("SSN", new Font(Font.HELVETICA, 12,
Font.BOLD)));
row.add(cell1);
//end column 3
//column 4
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.setWidth(6f);
// cell1.setWidthpercentage(10f);
cell1.add(new Paragraph("Code", new Font(Font.HELVETICA, 12,
Font.BOLD)));
row.add(cell1);
//end column 4
//column 5
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.setWidth(23f);
// cell1.setWidthpercentage(20f);
cell1.add(new Paragraph("Desc", new Font(Font.HELVETICA, 12,
Font.BOLD)));
row.add(cell1);
//end column 5

// column 6
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.setWidth(38f);

cell1.add(new Paragraph("Notes", new Font(Font.HELVETICA, 12,
Font.BOLD)));
row.add(cell1);
//end column 6
table1.addElement(row);
//end row 2

try {
data.getDBSelect().first();
int size = data.getDBSelect().getRowCount();
int j = 1;
for (int i = 0; i < size; i++) {
setFName(data.getDBSelect().getColumnAsString(5).trim());
setMName(data.getDBSelect().getColumnAsString(6).trim());
setLName(data.getDBSelect().getColumnAsString(4).trim());
// for (int i = 0; i < 100; i++) {
row = new SimpleCell(SimpleCell.ROW);
switch (i % 2) {
//switch (i % 3) {
case 0:
//row.setBackgroundColor(Color.red);
break;
case 1:
row.setBackgroundColor(Color.gray);
break;
case 2:
//row.setBackgroundColor(Color.blue);
break;
}
if (i % 2 == 1) {

}
if(i==20){

}
cell1 = new SimpleCell(SimpleCell.CELL);

cell1.add(new Paragraph((i + 1) + ".", new Font(
Font.HELVETICA, 9)));

row.add(cell1);

cell1 = new SimpleCell(SimpleCell.CELL);
cell1.add(new Paragraph(getLName()
+ " "
+ getFName()
+ " "
+ getMName(),
new Font(Font.HELVETICA, 9)));
row.add(cell1);
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.add(new Paragraph(data.getDBSelect()
.getColumnAsString(2).trim(), new Font(
Font.HELVETICA, 9)));
//cell1.setBackgroundColor(Color.gray);
row.add(cell1);
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.add(new Paragraph(data.getDBSelect()
.getColumnAsString(1).trim(), new Font(
Font.HELVETICA, 9)));
row.add(cell1);
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.add(new Paragraph(data.getDBSelect()
.getColumnAsString(7).trim(), new Font(
Font.HELVETICA, 9)));
row.add(cell1);
cell1 = new SimpleCell(SimpleCell.CELL);
cell1.add(new Paragraph(data.getDBSelect()
.getColumnAsString(3).trim(), new Font(
Font.HELVETICA, 9)));
row.add(cell1);
//}
table1.addElement(row);
// get next record
data.getDBSelect().next();

}
document.add(table1);

} catch (DBException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

// step 4

document.add(new Paragraph("\n" + new Date().toString(), new Font(
Font.HELVETICA, 10, Font.BOLD | Font.ITALIC)));
} catch (DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}

// step 5: we close the document (the outputstream is also closed
// internally)
document.close();

}


***one final note this servlet works perfect if a get request is made to it with the parameters all set in hte url.

thanks for your help
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only problem is that the servlet doesn't open it's itext document but rather sends it back to the client application.

I don't quite understand what the problem is. The servlet sends back a PDF document, but that's not what you were expecting? You're sending presentationtype=pdf, which instructs the servlet to send back a PDF. What did you expect instead?

Also, please edit your post and add "CODE" tags around it. It's very hard to read as it is: UseCodeTags
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!