简体   繁体   中英

Passing a normal java file value to jsp file

I have written a code to import an excel file where I get data and I want to insert data in my JSP file. Can I do it by using the normal code rather than using servlets? My code for upload the excel file is

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;

import org.apache.poi.hssf.record.formula.functions.Goto;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;



public class uploadexcel {
public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.println("Please enter a filename with extention to upload");

    String fileName = "C:\\"+scanner.nextLine();
    Vector dataHolder = ReadCSV(fileName);
    printCellDataToConsole(dataHolder);
} 

private static void printCellDataToConsole(Vector dataHolder) {
    // TODO Auto-generated method stub
    for(int i=0;i<dataHolder.size();i++)
    {
        Vector column=(Vector)dataHolder.elementAt(i);
        for (int j = 0; j < column.size(); j++) {
            HSSFCell myCell = (HSSFCell) column.elementAt(j);
            String stringCellValue = myCell.toString();

            System.out.print(stringCellValue + "\t");
    }
    System.out.println();
    }
}

private static Vector ReadCSV(String fileName) {
    // TODO Auto-generated method stub
     Vector cellVectorHolder = new Vector();
     try {
         FileInputStream myInput = new FileInputStream(fileName);

         POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

         HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

         HSSFSheet mySheet = myWorkBook.getSheetAt(0);

         Iterator rowIter = mySheet.rowIterator();

         while (rowIter.hasNext()) {
                 HSSFRow myRow = (HSSFRow) rowIter.next();
                 Iterator cellIter = myRow.cellIterator();
                 Vector column = new Vector();
                 while (cellIter.hasNext()) {
                         HSSFCell myCell = (HSSFCell) cellIter.next();
                         column.addElement(myCell);
                 }
                 cellVectorHolder.addElement(column);
         }
 } catch(IOException ie)
 {
     System.err.println("Please enter a valid input");

 }
     catch (Exception e) {
         e.printStackTrace();
 }
 return cellVectorHolder;
}

}

My jsp file is

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>NAMES ON THE LIST:</title>
</head>
<body>
names: <input type="text" name="firstname"><br>
</body>
</html>

You do realize that JSP is evaluated on the server and then the HTML result is passed to the browser?

In essence you could have this fragment in your JSP page:

<%  printCellDataToConsole( ReadCSV( "file.csv" ) ) %>

which would include your csv in the webpage, modulus markup...

Cheers,

I would suggest you not do in this way. Calling Servle t is the proper way to accomplish this.

But, You can do it this way.

<%@page import="java.util.Vector"%>

<%
 Vector oRetVal = new Vector();
 try{
       oRetVal = uploadexcel.ReadCSV("C:\\test.csv"); // Just an example
    }catch(Exception e){

        e.printStackTrace();
    }
%>

Iterate oRetVal in the JSP.

Note : Unrelated to the question: Read on Java Naming Conventions .
It should be "UploadExcel".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM