【问题标题】:Read the data from the CSV File and it should automatically generate the EMAIL_ID to the JSP Page从 CSV 文件中读取数据,它应该会自动生成 EMAIL_ID 到 JSP 页面
【发布时间】:2016-07-13 10:57:53
【问题描述】:

我有两个文本框,分别是LVPL_nameEmail_ID

如果我从LVPL_name 的下拉列表中选择任何名称,它应该会自动从 CSV 文件生成相应的电子邮件 ID,并将其显示在 JSP 页面中。

我的示例代码:

try { 
    String fileName = "LVPL_names.csv";
    CSVReader reader = new CSVReader(new FileReader(fileName ));
    String[] rows = new CSVReader(reader).readEmail_ID();
    if (x == Aarthi) { 
        System.out.println("Selected LVPL_name"+name.index of (Email_ID));
    } else { 
        System.out.println("Enter the Email_ID"); 
    } 
} catch() {

}

我的 Excel 表格是:

 LVPL_name   Email ID 
-----------------------
aaaaa1      aaaaa1@sap.com 
aaaaa2      aaaaa2@sap.com 
aaaaa3      aaaaa3@sap.com 
aaaaa4      aaaaa4@sap.com 
aaaaa5      aaaaa5@sap.com 

我认为我的代码有问题。

【问题讨论】:

  • @RadhikaEmmadi 在未来,请edit 将代码和数据添加到您的问题中,并在其下方添加“编辑”链接。

标签: java jsp


【解决方案1】:

您必须通过 ajax / servlet / xhr 请求来执行此操作。这是xhr 请求的示例。

要使以下代码正常工作(未编辑),您必须将 CSV 文件放在 C: 驱动器中。您的 CSV 文件名必须是 mydata.csv。此外,您应该将index.jspgetEmail.jsp 放在同一个文件夹中。

index.jsp

<%@page import="java.io.File"%>
<%@page import="java.io.FileReader"%>
<%@page import="com.opencsv.CSVReader"%>
<%@page import="java.util.ArrayList"%>

<html>
    <head>
        <script>
            function selected(value){
                var xhReq = new XMLHttpRequest();
                xhReq.open("GET", "getEmail.jsp?lvpl=" + value, false);
                xhReq.send(null);
                var serverResponse = xhReq.responseText;
                document.getElementById('textbox').value = serverResponse;
            }
        </script>
    </head>

    <body>

<%
    // Varaibles
    String lvpl, email;
    int counter = 0;
    CSVReader reader = new CSVReader(new FileReader(new File("C://mydata.csv")));
    String[] data;
%>

        <select onchange="selected(this.value)">
<%
    // READING TO CSV TO CREATE DATA IN SELECT BOX
    while ((data = reader.readNext()) != null) {

        // Skips CSV File Headings: LVPL and EMAIL_ID 
        if(counter == 0){
            counter++;      // Increase it so that it can't come in this block again
            continue;
        }

        // CSV FILE DATA AS READ BY CSVREADER
        lvpl = data[0];         // LVPL
        email = data[1];        // EMAIL
%>
            <option><%=lvpl%></option>
<%      
    }
%>
        </select>

        <input id="textbox" type="text" placeholder="email will show in here">

    </body>
</html>

getEmail.jsp

<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%@page import="com.opencsv.CSVReader"%>
<%@page import="java.util.ArrayList"%>
<%
    // get lvpl from received request
    String lvplReceived = request.getParameter("lvpl");

    // Reading CSV
    String lvpl = "", email = "";
    int counter = 0;
    boolean lvplFound = false;  // becomes true when lvpl matches selected lvpl

    CSVReader reader = new CSVReader(new FileReader(new File("C://mydata.csv")));
    String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {

        /* Skips CSV File Headers: LVPL and EMAIL_ID */
        if(counter == 0){
            counter++;
            continue;
        }

        // CSV FILE DATA
        lvpl = nextLine[0];         // LVPL
        email = nextLine[1];        // EMAIL

        if(lvpl.equals(lvplReceived)){      // if this lvpl matches with selected lvpl
            lvplFound = true;
            break;
        }

    }

    if(lvplFound){      // print email of matched lvpl
        out.print(email);
    } else {
        out.print("not found");
    }
%>

用 mydata.csv 测试

LVPL    EMAIL
aaaaa1  aaaaa1@sap.com
aaaaa2  aaaaa2@sap.com
aaaaa3  aaaaa3@sap.com
aaaaa4  aaaaa4@sap.com
aaaaa5  aaaaa5@sap.com

如果这不是您想要的方式,或者如果您不理解此代码,请随时通过 cmets 与我联系

有用的链接:

注意:如果您现在正在学习 JSP,我建议您转到 JSTL。 JSP 已不再使用,并且在 10 年前被弃用,因为它无法与 Microsoft 的 ASP 匹配。如果您继续学习 JSP,它将不会使您受益,尤其是在印度,我不敢相信那里的人们仍在教授 10 年的技术。这是一个JSTL book,可以帮助您入门。

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 2015-01-12
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多