• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to retrieve the data from a jsp page for print?

 
Maria James
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a dynamic jsp file which will get the data at runtime.I have a print button in the jsp.When i chick the print button it should capture the data from the jsp and print it as LANDSCAPE mode. Now i am using Java Api for this

Following code is a working sample which prints some data in LANDSCAPE mode..This is working fine....How can i modify this to get the data from a jsp?....I am using weblogic workshop for development


package Awtjava;
//working example

import java.awt.*;
import java.awt.print.*;

public class testPrint implements Printable {
private static String s[];
private static Font f;
private static int nLines;
public static void main (String arg[]){
s = new String[10];
f = new Font("Arial", Font.PLAIN, 11);
int i;
s[0] = "This is a printer test";
for (i=1; i<10; i++){
s[i] = "The next line";
}
nLines = 10;
testPrint.printa();
}
public static void printa(){
System.out.println("public static void printa () ");
PrinterJob pj=PrinterJob.getPrinterJob();
Book b = new Book();;
PageFormat pf = new PageFormat();
pf = pj.defaultPage(pf);
b.append(new testPrint(), pf, 2);
pj.setPageable (b);
try{
if (pj.printDialog()){
pj.print();
}
}
catch (Exception e) { System.out.println ("Error"); }
}
public int print (Graphics g1, PageFormat pf, int n){
System.out.println("public int print");
int x,y,i,fa,fh;
FontMetrics fm;
Graphics2D g = (Graphics2D) g1;
pf.setOrientation(PageFormat.LANDSCAPE);

fm = g.getFontMetrics (f);
fa = fm.getMaxAscent();
fh = fm.getHeight();
x = (int)pf.getImageableX();
y = (int)pf.getImageableY() + fa;

g.setFont (f);
g.setColor(Color.black);
if (n==1){
for (i=0; i<10; i++){
g.drawString (s[i], x, y);
y += fh;
}
return PAGE_EXISTS;
}
else {
for (i=10;i<10; i++){
g.drawString (s[i], x, y);
y += fh;
}
return PAGE_EXISTS;
}
}
}


Hope you will reply soon...
Thanks in advance
Maria
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maria,

Usually you would have the JSP get the data from a bean or you could even have it get the data directly from a database. Depends on what you are trying to do, how scalable it needs to be, etc. JavaRanch has a JSP forum here. I am moving this topic to that forum.
 
Paul Clapham
Sheriff
Posts: 21865
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally a JSP produces HTML which is laid out by the browser. Then you can just click on the "print" button in the browser and print the result of the JSP, in landscape format if you like.

Now you are suggesting you want to print the result of the JSP without the browser's help? Well, here's what you have to do:

1. Use a java.net.URL object to get the HTML produced by the JSP. (Should be easy, read this tutorial: http://java.sun.com/docs/books/tutorial/networking/urls/)

2. Lay out that HTML in a graphical component in the same way the browser would have done it. (May not be so easy, if your HTML is not too complex then you can use a JEditorPane; it has a mode that supports HTML 3.2 but not CSS or Javascript)

3. Print. (Looks like you know this part)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!