I am new to Eclipse. I am trying to run a jsp example using iText to generate a PDF, I put iText.jar file in Referense Library folder, but when I ran the jsp, I got error message that the class Document defined in the iText,jar can't not be resolved.
Right click on your project folder in the navigator, choose properties, choose java build path, select the libraries tab, click Add External Jars..., choose your jar. Click OK.
You can also add this jar into the project lib folder(present in WEB-INF, if not present you can add a folder in WEB-INF).
Now you can either
Right click on your project folder in the navigator, choose properties, choose java build path, select the libraries tab, click Add External Jars..., choose your jar. Click OK.
or
Right click on your project folder in the navigator, choose properties, choose java build path, select the libraries tab, click Add Jars..., choose your jar. Click OK.
Hope this helps..
- Add the jar in WEB-INF/lib
- add
<%@ page import="com.lowagie.itext.Document" %>
(or whatever the package is) to the top of your JSP
Please save yourself from trouble: do not write Java code in a JSP file, but just write it in a Java class.
You can use a Servlet
to preprocess requests.
package servlets;
import com.lowagie.itext.Document;
public class PdfSerlvet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Document document = doWhateverWithItextYouWant();
...
InputStream pdfContent = generateItSomehow();
...
response.setContentType("application/pdf");
response.setHeader("content-disposition", "inline;\"filename.pdf\"");
...
// Write pdfContent to response.getOutputStream() the usual Java IO way.
}
}
If necessary you can pass the document identifier as request parameter (obtainable by request.getParameter("name")
) or pathinfo (obtainable by request.getPathInfo()
) so that the servlet knows which PDF to return. Finally map this servlet on an url-pattern
something like as /pdf/*
so that you can access it by http://example.com/contextname/pdf/filename.pdf
(in this example the file identifier is passed as pathinfo, this gives much better results in MSIE).
Hope this helps. You can at least get more insights and code examples out of this http://balusc.blogspot.com/2007/07/fileservlet.html