Skocz do zawartości

[Java] FOP postawienie na serwerze


Jacob

Recommended Posts

 

Witam, mam pewien kod który powinien działać jako servlet projektu Apache FOP do konwertowania XML+XSL:fo do postaci PDF, sam od siebie stworzyłem Applet który działa, nawet na serwerze, jednakże problem w nim jest dość specyficzny, tzn. trzeba potwierdzać użycie każdej biblioteki co przy 13 daje ładną liczbę klikania OK...

Dołączyłem do Eclipse wszelkie potrzebne biblioteki, plik nie pokazuje żadnych błędów, po odpaleniu pliku CreatePDF.java, dostaje komunikat "The selection cannot be launched, and there are no recent launches" Oczywiście sprawdziłem co to za błąd, wchodzę do Run Configurations, wybieram Java Application, no i tu jest pies pogrzebany co wybrać jako main class aby to działało ?

Poniżej przesyłam kod źródłowy:

package servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;

//FOP
import org.apache.fop.servlet.ServletContextURIResolver;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class CreatePDF extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private FopFactory fopFactory ;

	private TransformerFactory tFactory ;

	/** URIResolver for use by this servlet */
    protected ServletContextURIResolver uriResolver;

	/**
	 * Constructor of the object.
	 */
	public CreatePDF() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {

			 //Setup a buffer to obtain the content length
		    ByteArrayOutputStream out = new ByteArrayOutputStream();

		    //tell the FOPFactory object where to look for resources
		    fopFactory.setURIResolver(uriResolver);

		    //Setup FOP to create a PDF and send it to the out
		    //ByteArrayOutputStream object
		    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

		    //Setup Transformer
		    //use the uriResolver object to find the XSL style sheet
		    Source xsltSrc = this.uriResolver.resolve(
		    	    "servlet-context:/xsl/peopleconvertor.xsl", null);

		    //tell the TransformerFactory where to find resources
		    tFactory.setURIResolver(uriResolver);

		    Transformer transformer = tFactory.newTransformer(xsltSrc);

		    //tell the Transformer object where to find resources
		    transformer.setURIResolver(uriResolver);

		    //Make sure the XSL transformation's result is piped through to FOP
		    Result res = new SAXResult(fop.getDefaultHandler());

		 
		    //use the uriResolver object to find the xml source file
		    Source xmlSrc = uriResolver.resolve(
		    	    "servlet-context:/xml/people.xml", null);

		    //Start the transformation and rendering process
		    transformer.transform(xmlSrc, res);

		    //Prepare response
		    response.setContentType("application/pdf");
		    response.setContentLength(out.size());

		    //Send content to Browser
		    response.getOutputStream().write(out.toByteArray());
		    response.getOutputStream().flush();

			} catch (Exception e) {

		          e.printStackTrace(System.err);

		    }

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 *
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet( request, response );
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {

		////specify that resources can be found in this Web application
		uriResolver = new ServletContextURIResolver(getServletContext());

		fopFactory = FopFactory.newInstance();

		tFactory = TransformerFactory.newInstance();

	}

}
Link do komentarza
Udostępnij na innych stronach

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...