Java 如何在JSP中显示表中的多行?
这是我的Servlet代码:Java 如何在JSP中显示表中的多行?,java,html,jsp,Java,Html,Jsp,这是我的Servlet代码: while(rs.next()){ int questionId = rs.getInt("question_id"); String questions = rs.getString("questions_name"); String option1 = rs.getString("option1"); String option2 = rs.getString("option2"); String option3 = rs
while(rs.next()){
int questionId = rs.getInt("question_id");
String questions = rs.getString("questions_name");
String option1 = rs.getString("option1");
String option2 = rs.getString("option2");
String option3 = rs.getString("option3");
String option4 = rs.getString("option4");
String correctAns = rs.getString("correct_ans");
request.setAttribute("questionId", questionId);
request.setAttribute("questions", questions);
request.setAttribute("option1", option1);
request.setAttribute("option2", option2);
request.setAttribute("option3", option3);
request.setAttribute("option4", option4);
request.setAttribute("correctAns", correctAns);
}
这是我的DAO代码:
public ResultSet StartTest(Test passData) throws SQLException{
Statement myStatement = getConnection();
String query;
query = "SELECT question_id, questions_name, option1, option2, option3, option4, correct_ans "
+ "FROM question WHERE courses_codes = '"+passData.getCourseCode()+"'"
+ "ORDER BY RAND()"
+ "LIMIT 5";
rs = myStatement.executeQuery(query);
return rs;
}
JSP代码:
${问题}${b}
${option1}${b}
${option2}${b}
${option3}${b}
${option4}${b}
${correctAns}${b}
在servlet中使用此代码
HttpSession session=request.getSession();
Arraylist<Person>listPerson=daoPerson.getLisPersons();
session.setAttribute("lsperson",listPerson);
HttpSession session=request.getSession();
ArrayListPerson=daoPerson.getLisPersons();
session.setAttribute(“lsperson”,listPerson);
在jsp页面中导入jstl标记库
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
然后把这个密码
<c:forEach items="${lsperson}" var="person">
<tr>
<td>${person.name}</td>
<td>${person.lastName}</td>
<td>${person.age}</td>
</tr>
</c:forEach>
${person.name}
${person.lastName}
${person.age}
是否有多行?意思是使用JSP页面中的循环,但您能给我一个如何使用它的示例,比如一个想法吗。感谢您的回答,但我仍然对servlet部分感到困惑,因为我需要得到7个项目,我将把它们传递到jsp页面,我如何通过lsperson将这7个项目传递到jsp页面,就像您给我的回答一样。谢谢