简体   繁体   中英

How can I change this code to a servlet page with action control from a jsp page?

I need this java code to be run in a servlet page. So that when I submit a button in jsp it should display the values.

Below given is a java code which have a main function.

public static void main(String[] args) 
    { 
        try { 
            FileInputStream file = new FileInputStream(new File("userData.xlsx"));       
            XSSFWorkbook workbook = new XSSFWorkbook(file);            
            Sheet sheet = workbook.getSheetAt(0); 
            Iterator<Row> rowIterator = sheet.iterator(); 
            while (rowIterator.hasNext()) { 
                Row row = rowIterator.next(); 
                Iterator<Cell> cellIterator = row.cellIterator(); 

                while (cellIterator.hasNext()) { 
                    Cell cell = cellIterator.next(); 
                    switch (cell.getCellType()) { 
                    case NUMERIC: 
                        System.out.print(cell.getNumericCellValue()); 
                        break; 
                    case STRING: 
                        System.out.print(cell.getStringCellValue());                            
                        break; 
                    } 
                } 
                System.out.println(""); 
            } 
            file.close(); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } }}

I need to run this code in a servlet and give its url in a jsp page.

As I understand your query, that on a http get request, read the excel file's first sheet and return the records.

You can use spring's web mvc framework, where you may perform your reading excel file action in controller and return the excel view.

I hope this tutorial may help.

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