我有两个文本框,即LVPL_name
和Email_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
我觉得我的代码有问题。
答案 0 :(得分:2)
您必须通过ajax
/ servlet
/ xhr
请求执行此操作。以下是xhr
请求的示例。
要使代码与(未经编辑)一起正常运行,您必须将{CSV}文件放在C:
驱动器中。您的CSV文件名必须为mydata.csv
。而且,您应该将index.jsp
和getEmail.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
如果这不符合您的要求,或者您不理解此代码,请随时通过评论与我联系
有用的链接:
注意:如果您现在正在学习JSP,我建议您转到JSTL。 JSP已不再使用,并且在10年前被弃用,因为它与微软的ASP不匹配。如果你继续学习JSP,它将来不会让你受益,特别是在印度,我不相信那里的人仍然在教一个已有10年历史的技术。这是一个JSTL book,可以帮助您入门。