Servlet example for implementing PDF documents

Source: Internet
Author: User

Package appointmanager;

Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. util .*;
// Import net. SF. jasperreports. Engine. *; //. Jasper. Engine .*;
Import net. SF. jasperreports. Engine. jrexception;
Import net. SF. jasperreports. Engine. jrruntimeexception;
Import net. SF. jasperreports. Engine. jasperfillmanager;
Import net. SF. jasperreports. Engine. jasperprint;
Import net. SF. jasperreports. Engine. jasperrunmanager;
Import util .*;
Import java. SQL .*;

/**
* <P> title: lims B/S </P>
* <P> Description: lims B/S revision </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: labtech </P>
* @ Author Xiong qinxue
* @ Version 1.1
*/

Public class getappointreport extends httpservlet {

Private Static final string content_type = "text/html; charset = gb2312 ";
// Initialize global variables
Public void Init () throws servletexception {
}
// Process the http get request
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
String id = request. getparameter ("ID ");
If (ID = NULL ){
Id = "";
}

Try {
Dbutils datasource = new dbutils ();
Connection conn = datasource. conn_odbc ();
Servletcontext = This. getservletcontext ();
// File reportfile = new file (servletcontext. getrealpath ("/WEB-INF/classes/report/testcontract. Jasper"); // newappoint. Jasper
File reportfile = new file (servletcontext. getrealpath ("/WEB-INF/classes/report/appointinfo. Jasper "));
If (! Reportfile. exists ())
Throw new jrruntimeexception ("file datasourcetest. Jasper not found. The report design must be compiled first .");
Map parameters = new hashmap ();
// Parameters. Put ("wtbh", ID); // set parameters for the report
Parameters. Put ("ID", ID );
Byte [] bytes = jasperrunmanager. runreporttopdf (reportfile. getpath (), parameters, Conn );
System. Out. println ("ID =" + ID + ";" + reportfile. getpath ());
Response. setcontenttype ("application/pdf ");
Response. setcontentlength (bytes. Length );
Servletoutputstream ouputstream = response. getoutputstream ();
Ouputstream. Write (bytes, 0, bytes. Length );
Ouputstream. Flush ();
Ouputstream. Close ();
}
Catch (exception e ){
Response. setcontenttype (content_type );
Printwriter out = response. getwriter ();
Out. Print (E. getmessage ());
}
}
// Process the http post request
Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
Doget (request, response );
}
// Clean up resources
Public void destroy (){
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.