简体   繁体   English

如何通过jsp页面将此代码更改为具有动作控制的servlet页面?

[英]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. 我需要此Java代码在servlet页面中运行。 So that when I submit a button in jsp it should display the values. 这样,当我在jsp中提交按钮时,它应该显示值。

Below given is a java code which have a main function. 以下是具有主要功能的Java代码。

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. 我需要在servlet中运行此代码,并在jsp页面中提供其url。

As I understand your query, that on a http get request, read the excel file's first sheet and return the records. 据我了解您的查询,即在http get请求上,阅读excel文件的第一张表并返回记录。

You can use spring's web mvc framework, where you may perform your reading excel file action in controller and return the excel view. 您可以使用spring的Web MVC框架,在其中可以在控制器中执行读取excel文件的操作并返回excel视图。

I hope this tutorial may help. 希望本教程对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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