I have problem, I need to know how to input data form JSP to servlet PDF.

Now, I can set PDF. I have source code.

 

PDFServlet.java

package pdfservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

// import the iText packages
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/**
 *
 *  a servlet that will generate a PDF document
 *  and send the document to the client via the
 *  ServletOutputStream
 *
 *  @author Sean C. Sullivan
 *
 */
public class PDFServlet extends HttpServlet
{
 /**
 *
 *
 */
 public PDFServlet()
 {
  super();
 }

 /**
  * 
  *
  * we implement doGet so that this servlet will process all
  * HTTP GET requests
  *
  * @param req HTTP request object
  * @param resp HTTP response object
  *
  */
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws javax.servlet.ServletException, java.io.IOException
 {
  DocumentException ex = null;
  
  ByteArrayOutputStream baosPDF = null;
  
  try
  {
   baosPDF = generatePDFDocumentBytes(req, this.getServletContext());
   
   StringBuffer sbFilename = new StringBuffer();
   sbFilename.append("filename_");
   sbFilename.append(System.currentTimeMillis());
   sbFilename.append(".pdf");

   ////////////////////////////////////////////////////////
   // Note:
   //
   // It is important to set the HTTP response headers
   // before writing data to the servlet's OutputStream
   //
   ////////////////////////////////////////////////////////
   //
   //
   // Read the HTTP 1.1 specification for full details
   // about the Cache-Control header
   //
   resp.setHeader("Cache-Control", "max-age=30");
   
   resp.setContentType("application/pdf");
   
   //
   //
   // The Content-disposition header is explained
   // in RFC 2183
   //
   //    http://www.ietf.org/rfc/rfc2183.txt
   //
   // The Content-disposition value will be in one of
   // two forms:
   //
   //   1)  inline; filename=foobar.pdf
   //   2)  attachment; filename=foobar.pdf
   //
   // In this servlet, we use "inline"
   //
   StringBuffer sbContentDispValue = new StringBuffer();
   sbContentDispValue.append("inline");
   sbContentDispValue.append("; filename=");
   sbContentDispValue.append(sbFilename);
       
   resp.setHeader(
    "Content-disposition",
    sbContentDispValue.toString());

   resp.setContentLength(baosPDF.size());

   ServletOutputStream sos;

   sos = resp.getOutputStream();
   
   baosPDF.writeTo(sos);
   
   sos.flush();
  }
  catch (DocumentException dex)
  {
   resp.setContentType("text/html; charSet=TIS620");
   PrintWriter writer = resp.getWriter();
   writer.println("<meta http-equiv='Content-Type' content='text/html; charset=TIS-620'>");
   writer.println("������ ������");
   writer.println(
     this.getClass().getName()
     + " 111111111111111110000000000000000 ����� "
     + dex.getClass().getName()
     + "<br>");
   writer.println("<pre>");
   dex.printStackTrace(writer);
   writer.println("</pre>");
  }
  finally
  {
   if (baosPDF != null)
   {
    baosPDF.reset();
   }
  }
  
 }

 /**
  * 
  * @param req must be non-null
  *
  * @return a non-null output stream. The output stream contains
  *         the bytes for the PDF document
  *
  * @throws DocumentException
  *
  */
 protected ByteArrayOutputStream generatePDFDocumentBytes(
  final HttpServletRequest req,
  final ServletContext ctx)
  throws DocumentException
  
 { 
  Document doc = new Document();
  
  ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
  PdfWriter docWriter = null;

  try
  {
   docWriter = PdfWriter.getInstance(doc, baosPDF);
   
   doc.addAuthor(this.getClass().getName());
   doc.addCreationDate();
   doc.addProducer();
   doc.addCreator(this.getClass().getName());
   doc.addTitle("This is a title.");
   doc.addKeywords("pdf, itext, Java, open source, http");
   
   doc.setPageSize(PageSize.LETTER);
  
String omyEname="123jsjjˡˡ�";
//String myEname = new String(omyEname.getBytes('ISO8859_1'),'TIS-620');
//String myEname = new String(.getString ("fdfd�ˡˡˡ�").getBytes("ISO8859_1"),"TIS-620");

 

   HeaderFooter footer = new HeaderFooter(
       new Phrase("dsdsd�ˡˡomyEname."),
       false);

   doc.setFooter(footer);
  
   doc.open();
      

   doc.add(new Paragraph(
      "This document was created by a class named: "
      + this.getClass().getName()));
      
   doc.add(new Paragraph(
      "This document was created on "
      + new java.util.Date()));

   String strServerInfo = ctx.getServerInfo();
   
   if (strServerInfo != null)
   {
    doc.add(new Paragraph(
      "Servlet engine: " + strServerInfo));
   }
   
   doc.add(new Paragraph(
      "This is a multi-page document."));
   
   doc.add( makeGeneralRequestDetailsElement(req) );
   
   doc.newPage();
   
   doc.add( makeHTTPHeaderInfoElement(req) );
   
   doc.newPage();
   
   doc.add( makeHTTPParameterInfoElement(req) );
   
  }
  catch (DocumentException dex)
  {
   baosPDF.reset();
   throw dex;
  }
  finally
  {
   if (doc != null)
   {
    doc.close();
   }
   if (docWriter != null)
   {
    docWriter.close();
   }
  }

  if (baosPDF.size() < 1)
  {
   throw new DocumentException(
    "document has "
    + baosPDF.size()
    + " bytes");  
  }
  return baosPDF;
 }
 
 /**
  *
  * @param req HTTP request object
  * @return an iText Element object
  *
  */
 protected Element makeHTTPHeaderInfoElement(final HttpServletRequest req)
 {
  Map mapHeaders = new java.util.TreeMap();
  
  Enumeration enumHeaderNames = req.getHeaderNames();
  while (enumHeaderNames.hasMoreElements())
  {
   String strHeaderName = (String) enumHeaderNames.nextElement();
   String strHeaderValue = req.getHeader(strHeaderName);
   
   if (strHeaderValue == null)
   {
    strHeaderValue = "";
   }
   mapHeaders.put(strHeaderName, strHeaderValue);
  }

  Table tab = makeTableFromMap(
    "HTTP header name",
    "HTTP header value",
    mapHeaders);
  
  return (Element) tab;
 }

 /**
  * 
  * @param req HTTP request object
  * @return an iText Element object
  *
  */
 protected Element makeGeneralRequestDetailsElement(
      final HttpServletRequest req)
 {
  Map mapRequestDetails = new TreeMap();
  
  mapRequestDetails.put("Scheme", req.getScheme());
    
  mapRequestDetails.put("HTTP method", req.getMethod());
    
  mapRequestDetails.put("AuthType", req.getAuthType());
    
  mapRequestDetails.put("QueryString", req.getQueryString());
    
  mapRequestDetails.put("ContextPath", req.getContextPath());
    
  mapRequestDetails.put("Request URI", req.getRequestURI());
    
  mapRequestDetails.put("Protocol", req.getProtocol());
    
  mapRequestDetails.put("Remote address", req.getRemoteAddr());
    
  mapRequestDetails.put("Remote host", req.getRemoteHost());
    
  mapRequestDetails.put("Server name", req.getServerName());
    
  mapRequestDetails.put("Server port", "" + req.getServerPort());
    
  mapRequestDetails.put("Preferred locale", req.getLocale().toString());
    
  Table tab = null;
  
  tab = makeTableFromMap(
      "Request info",
      "Value",
      mapRequestDetails);
  
  return (Element) tab;
 }

 /**
  *
  *
  * @param req HTTP request object
  * @return an iText Element object
  *
  */
 protected Element makeHTTPParameterInfoElement(
     final HttpServletRequest req)
 {
  Map mapParameters = null;
  
  mapParameters = new java.util.TreeMap(req.getParameterMap());

  Table tab = null;

  tab = makeTableFromMap(
    "HTTP parameter name",
    "HTTP parameter value",
    mapParameters);
  
  return (Element) tab;
 }
 
 /**
  *
  * @param firstColumnTitle
  * @param secondColumnTitle
  * @param m map containing the data for column 1 and column 2
  *
  * @return an iText Table
  *
  */
 private static Table makeTableFromMap(
   final String firstColumnTitle,
   final String secondColumnTitle,
   final java.util.Map m)
 {
  Table tab = null;

  try
  {
   tab = new Table(2 /* columns */);
  }
  catch (BadElementException ex)
  {
   throw new RuntimeException(ex);
  }
  
  tab.setBorderWidth(1.0f);
  tab.setPadding(5);
  tab.setSpacing(5);

  tab.addCell(new Cell(firstColumnTitle));
  tab.addCell(new Cell(secondColumnTitle));
  
  tab.endHeaders();

  if (m.keySet().size() == 0)
  {
   Cell c = new Cell("none");
   c.setColspan(tab.columns());
   tab.addCell(c);
  }
  else
  {
   Iterator iter = m.keySet().iterator();
   while (iter.hasNext())
   {
    String strName = (String) iter.next();
    Object value = m.get(strName);
    String strValue = null;
    if (value == null)
    {
     strValue = "";
    }
    else if (value instanceof String[])
    {
     String[] aValues = (String[]) value;  
     strValue = aValues[0];
    }
    else
    {
     strValue = value.toString();
    }
    
    tab.addCell(new Cell(strName));
    tab.addCell(new Cell(strValue));
   }
  }
  
  return tab;
 }
 
}



 

and I have formServlet.jsp

 

<%@ page contentType="text/html; charset=windows-874" language="java" import="java.sql.*" errorPage="" %>
<HTML>
<HEAD>
  <TITLE>A Sample FORM using POST</TITLE>
</HEAD>

<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">A Sample FORM using POST</H1>

<FORM ACTION=""
      METHOD="POST">
  Item Number:
  <INPUT TYPE="TEXT" NAME="itemNum"><BR>
  Quantity:
  <INPUT TYPE="TEXT" NAME="quantity"><BR>
  Price Each:
  <INPUT TYPE="TEXT" NAME="price" VALUE="$"><BR>
  <HR>
  <p>First-Name:
    <INPUT TYPE="TEXT" NAME="first">
    <BR>
    Last-Name:
    <INPUT TYPE="TEXT" NAME="lastName">
    <BR>
    Middle :
    <INPUT TYPE="TEXT" NAME="initial">
  </p>
  <p>
  à¾Å§â»Ã´
    <INPUT TYPE="TEXT" NAME="song">
    <BR>
    Shipping Address:
    <TEXTAREA NAME="address" ROWS=3 COLS=40></TEXTAREA>
    <BR>
    Card Credit:<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Visa">
    Visa<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Master Card">
    Master Card<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Amex">
    American Express<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Discover">
    Discover<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Java SmartCard">
    Java SmartCard<BR>
    Credit Card Number:
    <INPUT TYPE="PASSWORD" NAME="cardNum">
    <BR>
    Repeat Credit Card Number:
    <INPUT TYPE="PASSWORD" NAME="cardNum">
    <BR>
    <BR>
  </p>
  <CENTER>
    <INPUT TYPE="SUBMIT" VALUE="Submit Order">
  </CENTER>
</FORM>

</BODY>
</HTML>

 

Thank you very much,

from

sad Girl

pretty from nuu_dao


Add photos to your e-mail with MSN 8. Get 2 months FREE*. ------------------------------------------------------- SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media 100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33 Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift. http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285 _______________________________________________ iText-questions mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to