views:

283

answers:

4

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.

A: 

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.

Asaph
Just what I did, the jar file shows in Referense Library folder. but the jsp can;t be compiled
Southsouth
@northTiger: How exactly are you compiling the jsp file? Usually the web container does that, not the IDE.
Asaph
there is red underline in IDE under Document document = new Document();
Southsouth
and I got warning" "Classpath entry C:/workspace/intro/WebContent/iText-2.1.7.jar will not be exported or published. Runtime ClassNotFoundExceptions may result."
Southsouth
@northTiger: You may need to simply right-click refresh the project. See this link: http://www.coderanch.com/t/448061/IDEs-Version-Control-other-tools/Eclipse-project-Jar-will-not .
Asaph
A: 

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..

Richie
Right click on your project folder in the navigator, choose properties, choose java build path, select the libraries tab, click Add Jars, Jar Selection dialog shows up,click wenContent folder, the jar file does not show up. no way to add jar this way
Southsouth
okies... 1st wat you have t o do is copy the jar file into WEB-INF/lib folder..then try webContent/WEB-INF/libThen try adding as jar..if this doesn work then try adding as ext jar file (after putting jar in webContet/WEB-INF/lib and pointig) to that file
Richie
A: 
  1. Add the jar in WEB-INF/lib
  2. add <%@ page import="com.lowagie.itext.Document" %> (or whatever the package is) to the top of your JSP
Bozho
This is wrong. 1. should be WEB-INF/lib
Chris Harcourt
Thanks. a dull mistake :)
Bozho
A: 

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

BalusC