Wednesday 21 May 2014

HTTP Request by using JSTL

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
  <head>
    <title>Page Data Example</title>
  </head>
  <body>
    <h3>&#160;</h3>
    <table border="1" width="539">
      <tr>
        <td colspan="2" width="529" bgcolor="#0000FF">
          <b>
            <font color="#FFFFFF" size="4">HTTP
            Request(pageContext.request.)</font>
          </b>
        </td>
      </tr>
      <tr>
        <td width="210">Access Method</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.method}" />
        </td>
      </tr>
      <tr>
        <td width="210">Authentication Type</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.authType}" />
        </td>
      </tr>
      <tr>
        <td width="210">Context Path</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.contextPath}" />
        </td>
      </tr>
      <tr>
        <td width="210">Path Information</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.pathInfo}" />
        </td>
      </tr>
      <tr>
        <td width="210">Path Translated</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.pathTranslated}" />
        </td>
      </tr>
      <tr>
        <td width="210">Query String</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.queryString}" />
        </td>
      </tr>
      <tr>
        <td width="210">Request URI</td>
        <td width="313">&#160;
        <c:out value="${pageContext.request.requestURI}" />
        </td>
      </tr>
    </table>
  </body>
</html>

Wednesday 14 May 2014

Reading xls and xlsx in java using POI jars

Below Jars are Required for implemnetation:
   dom4j-1.6.1.jar
   poi-3.10-FINAL-20140208.jar
   poi-ooxml-3.10-FINAL-20140208.jar
   poi-ooxml-schemas-3.10-FINAL-20140208.jar
   stax-api-1.0.1.jar
   xmlbeans-2.3.0.jar
   commons-lang-2.6.jar

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ExcelMainProgram {
 public static void main(String[] args) throws Exception {
  //
  // An excel file name. You can create a file name with a full path
  // information.
  //
  System.out.println(new Date());
  String filename = "Template.xls";//xls or xlsx
  //
  // Create an ArrayList to store the data read from excel sheet.
  //
  ArrayList<ArrayList<Cell>> sheetData = new ArrayList<ArrayList<Cell>>();
  FileInputStream fis = null;
  try {
   //
   // Create a FileInputStream that will be use to read the excel file.
   //
   fis = new FileInputStream(filename);
   //
   // Create an excel workbook from the file system.
   //
   Workbook workbook = WorkbookFactory.create(fis);;
   //
   // Get the first sheet on the workbook.
   //
   Sheet sheet = workbook.getSheetAt(0);
   //
   // When we have a sheet object in hand we can iterator on each
   // sheet's rows and on each row's cells. We store the data read
   // on an ArrayList so that we can printed the content of the excel
   // to the console.
   //
   Iterator<Row> rows = sheet.rowIterator();
   while (rows.hasNext()) {
    Row row =rows.next();
    if(row.getRowNum()==2)
    {continue;}
   }
   while (rows.hasNext()) {
    Row row =rows.next();
    Iterator<Cell> cells = row.cellIterator();
    System.out.println(row.getRowNum());
    ArrayList<Cell> data = new ArrayList<Cell>();
    while (cells.hasNext()) {
     Cell cell =  cells.next();
     data.add(cell);
    }
    sheetData.add(data);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (fis != null) {
    fis.close();
   }
  }
  showExelData(sheetData);
  System.out.println(new Date());
 }
 private static void showExelData(List<ArrayList<Cell>> sheetData) {
  //
  // Iterates the data and print it out to the console.
  //
  for (int i = 0; i < sheetData.size(); i++) {
   List<Cell> list = (List<Cell>) sheetData.get(i);
   for (int j = 0; j < list.size(); j++) {
    Cell cell =list.get(j);
    cell.setCellType(Cell.CELL_TYPE_STRING);//To get the cell value as string set the cell type as String
    System.out.print(cell.getStringCellValue() + "\t\t");
    if (j < list.size() - 1) {
     //System.out.print("SPACE");
    }
   }
   System.out.println("");
  }
 }
}

JavaScript/Ajax file upload with Jquery iframe post

Demo code
<script type="text/javascript" src="jQueryAjaxUpload.js"></script>

<form method="post" action="" enctype="multipart/form-data" id="example_form">
<input type="file" name="file"/>
<input type="submit"/>
</form>
<script>

$('#example_form').ajaxUpload({

post :
function (){

alert('posted');
},


complete :
function() {

alert('Upload complete');
}


});
</script> In case of using spring add below bean to spring beans
<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- one of the properties available; the maximum file size in bytes -->

<property name="maxUploadSize" value="1000000"/>
</bean>
 Follow the below link to download jQueryAjaxUpload.js file

http://www.jainaewen.com/files/javascript/jquery/iframe-post-form.html

Run Postman API remote or code or command line

POSTMAN is used to run the APIs generally. What if we want to create a collection in postman and run it from some other code or command l...