Excel to PDF Java Servlet - Configuration

This is the final part of the Excel to PDF table Java servlet series. In the first part we designed a HTML form that accepted XLS / XLSX files and submitted it to a servlet. In Part 2 of this series, we identified the JAR files required to produce the servlet code. We provide a fully commented version of the code in Part 3.  In the final part of the series, we will explain how to do some basic configuration in TomCat to get the servlet working.

Copy Servlet Files to TomCat

Once you have successfully compiled your servlet, you need to copy the file xlstopdfservlet.class to the directory - apache-tomcat-6.0.36\webapps\examples\WEB-INF\classes.

Edit Web-XML file

The next step is to edit web.xml file and add the following lines in appropriate places, as shown below:
    <servlet-mapping>
        <servlet-name>xlstopdfservlet</servlet-name>
        <url-pattern>/servlets/servlet/xlstopdfservlet</url-pattern>
    </servlet-mapping>    


    <servlet>
        <servlet-name>xlstopdfservlet</servlet-name>
        <servlet-class>xlstopdfservlet</servlet-class>
    </servlet>


Copy HTML Code to Server


We earlier created our HTML form code => form.html. Copy the HTML file to the following location => apache-tomcat-6.0.36\webapps\examples\servlets\xlstopdf


Change Form Action


In Part 1, if you recollect, we did not specify the “Action” against the form. It is time to specify that. Edit your form.html and specify the action as given below:


<form id="form_533088" class="appnitro" enctype="multipart/form-data" method="post" action="/examples/servlets/servlet/xlstopdfservlet">


If you have done all these steps properly, you can start tomcat and upload your excel file. You will see the PDF output ready!.The URL assuming you have installed in port 8080, is given below:


http://localhost:8080/examples/servlets/servlet/xlstopdfservlet.


That completes an interesting tutorial to convert Excel to PDF through a servlet. If you have any questions, you can post it in the comments section of this blog. Links to other parts of this tutorial, is provided below:
This Tutorial Series: Convert Excel to PDF – Java Servlet Example
Keywords: Excel to PDF, Java Servlet, Apache POI, Itext, Convert XLS to PDF Example
All parts:
1. HTML Form Design to support XLS to PDF Conversion.
2. JAR Files required for conversion and their use.
3. Handling uploaded file in a servlet./ Complete Java code to convert Excel to PDF through a servlet.
4. Servlet Configuration / Testing.

No comments:

Post a Comment