title: Error saying--->The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
body: <p>Below is the code to convert .xls/.xlsx files to .pdf</p>
  
  <p>When I try to hit the link 
  <a href="http://localhost:8080/examples/servlets/servlet/xlstopdfservlet" rel="nofollow noreferrer">http://localhost:8080/examples/servlets/servlet/xlstopdfservlet</a>  </p>
  
  <p>I get the following error
  <code>The origin server did not find a current representation for the target 
  resource or is not willing to disclose that one exists.</code></p>
  
  <h1>ExcelWord2PDF_HTML.html</h1>
  
  <pre class="lang-html prettyprint-override"><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;title&gt;XLS to PDF - Servlet Example&lt;/title&gt;
  &lt;/head&gt;
  &lt;body &gt;
  &lt;h1&gt;&lt;a&gt;HTML Form to Capture user Uploaded XLS File&lt;/a&gt;&lt;/h1&gt;
  &lt;form id="form_533088" class="appnitro" enctype="multipart/form-data" 
  method="post" action="/examples/servlets/servlet/xlstopdfservlet"&gt;
  &lt;ul &gt;
  &lt;li id="li_1" &gt;
  &lt;label for="element_1"&gt;Select an Excel File to Convert &lt;/label&gt;
  &lt;input id="element_1" name="element_1" type="file"/&gt; 
  &lt;/li&gt;
  &lt;li id="li_2" &gt;
  &lt;label for="element_2"&gt;Excel Input Format &lt;/label&gt;
  &lt;select class="element select medium" id="element_2" name="element_2"&gt; 
  &lt;option value="" selected="selected"&gt;&lt;/option&gt;
  &lt;option value="1" &gt;XLS&lt;/option&gt;
  &lt;option value="2" &gt;XLSX&lt;/option&gt;
  &lt;/select&gt;
  &lt;/li&gt;           
  &lt;li&gt;                
  &lt;input id="saveForm" class="button_text" type="submit" name="submit" 
  value="Submit" /&gt;
  &lt;/li&gt;
  &lt;/ul&gt;
  &lt;/form&gt; 
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <h1>xlstopdfservlet.java</h1>
  
  <pre class="lang-java prettyprint-override"><code>package com.infy.accelearte.excelword2pdf;
  
  
  public class xlstopdfservlet extends HttpServlet {
  
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
          OutputStream out = response.getOutputStream();  
  
          try {
                  //set InputStream object that accepts uploaded file
                  InputStream filecontent=null;              
                  List&lt;FileItem&gt; items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                  //assume default input type as XLS
                  String inputtype="1"; 
                  for (FileItem item : items) {
                          if (item.isFormField()) {                               
                                  String fieldname = item.getFieldName();
                                  if (fieldname.equals("element_2")) {
                                  //identifies type of input required
                                  inputtype = item.getString(); 
                                  }
                                  //the value can be 1 for XLS conversion, 2 for XLSX conversion
  
                          } else {
                          //The uploaded file is processed in this section
                          String fieldname = item.getFieldName();
                          String filename = FilenameUtils.getName(item.getName());
                          filecontent = item.getInputStream();
                          //Uploaded Excel file is obtained into Inputstream object at this step                
                          }
                  }
           //we have obtained the user uploaded Excel file till this step.
           // we also know the output is a PDF.
           // we can now write the servlet code that converts the file.
           HSSFWorkbook my_xls_workbook=null;
           HSSFSheet my_worksheet=null;
           XSSFWorkbook my_xlsx_workbook=null;
           XSSFSheet my_worksheet_xlsx=null;
  
           //set the output format i.e. pdf         
           response.setContentType("application/pdf");
           Document document = new Document();
           PdfWriter.getInstance(document, out);
           document.open();
           //we create a PDF table that can hold XLS data
           //assumption is excel sheet is holding two columns only.
           //you can make this dynamic
           PdfPTable my_table = new PdfPTable(2);
           PdfPCell table_cell;
           //the object below loops through the excel document rows
           Iterator&lt;Row&gt; rowIterator = null;
           //read xls
  
           if (inputtype.equals("1")){
           my_xls_workbook = new HSSFWorkbook(filecontent); 
           my_worksheet = my_xls_workbook.getSheetAt(0);
           rowIterator=my_worksheet.iterator();    
           }
  
           //read xlsx
           if (inputtype.equals("2")){
           my_xlsx_workbook = new XSSFWorkbook(filecontent); 
           my_worksheet_xlsx = my_xlsx_workbook.getSheetAt(0);
           rowIterator=my_worksheet_xlsx.iterator();       
           }
           while(rowIterator.hasNext()) {
                          Row row = rowIterator.next(); 
                          Iterator&lt;Cell&gt; cellIterator = row.cellIterator();
                                  while(cellIterator.hasNext()) {
                                          Cell cell = cellIterator.next(); //Fetch CELL
                                          switch(cell.getCellType()) { //Identify CELL type
  
                                          case Cell.CELL_TYPE_STRING:
                                                  //Push the data from Excel to PDF Cell
                                                   table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));                                                
                                                   my_table.addCell(table_cell);
                                                  break;
                                          }
                                          //next line
                                  }
  
                  }
          // add table to PDF document        
          document.add(my_table);     
          //close document..returns output to server
          document.close();
  
          }
          catch (Exception e) {
                 System.err.println(e.toString()); /* Throw exceptions to log files */
           }
           finally {
                   out.close();/* Close the output stream */
           }
  
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
      doGet(request, response);
  }
  </code></pre>
  
  <hr>
  
  <h1>web.xml</h1>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;ExcelWord_PDF&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
       &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
    &lt;servlet&gt;
          &lt;servlet-name&gt;xlstopdfservlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.infy.accelerate.excelword2pdf.xlstopdfservlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;xlstopdfservlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/servlets/servlet/xlstopdfservlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>